bash 从 shell 脚本运行 java 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22819319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
running java file from shell script
提问by bekur
I am new to run java files through shell script, this may be very basic question for those who are experienced or few knowledge in shell script. I have java file called Main.java under
我是通过 shell 脚本运行 java 文件的新手,对于那些有经验或对 shell 脚本知之甚少的人来说,这可能是非常基本的问题。我在下面有名为 Main.java 的 java 文件
C:\project\Tranmissions\com.abc.files\src\main\java\com\abc\files
+Main.java
I have shell script called run.sh:
我有一个名为 run.sh 的 shell 脚本:
#!/bin/bash
java -Xmx300m -classpath com.abc.files.Main -mainclass com.abc.files.payroll.f401k.xyz.AdpCwMain -driver org.hsqldb.jdbc.JDBCDriver
exit $?
This script I have placed under
这个脚本我放在下面
C:\project\Tranmissions\com.abc.files.
Now, I have downloaded cygwin to run the script as
现在,我已经下载了 cygwin 来运行脚本
./run.sh
When I run this, I am getting following basic java error:
当我运行它时,我收到以下基本的 Java 错误:
java.lang.NoClassDefFoundError: com/abc/files/Main
Caused by: java.lang.ClassNotFoundException: com.abc.files.Main
at java.net.URLClassLoader.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.abc.files.Main. Program will exit.
Exception in thread "main"
I am using STS(Eclipse) with maven command install to run the java files. and able to run my class "Main" java program.
我正在使用带有 maven 命令安装的 STS(Eclipse) 来运行 java 文件。并且能够运行我的类“Main”java程序。
回答by Paul Rubel
You shouldn't need to specify -mainclass
, just give the class with the main. Also the classpath has the com.abc prefix as does your class. You probably want the classpath to be the current dir and then give your class. If your JDBC isn't in the classpath you'll also get an error. Try something like so:
您不需要指定-mainclass
,只需提供带有 main 的类即可。此外,类路径与您的类一样具有 com.abc 前缀。您可能希望类路径是当前目录,然后提供您的类。如果您的 JDBC 不在类路径中,您也会收到错误消息。尝试这样的事情:
java -Xmx300m -classpath . \
com.abc.files.payroll.f401k.xyz.AdpCwMain \
-driver org.hsqldb.jdbc.JDBCDriver
Run with #!/bin/bash -x
to show what commands are actually executed.
运行#!/bin/bash -x
以显示实际执行的命令。
回答by j.con
You call "java" command on a file called Main.java, you should compile the Main.java class first with "javac". "java" is then used when the program ends in .class, ie Main.class
您在名为 Main.java 的文件上调用“java”命令,您应该首先使用“javac”编译 Main.java 类。然后当程序以 .class 结束时使用 "java",即 Main.class
回答by Sireesh Yarlagadda
You are not compiling the code , before you execute the line.
在执行该行之前,您没有编译代码。
javac line is missing in run.sh
run.sh 中缺少 javac 行
Why dont you try j2sch, it make your life even simpler.
为什么不试试 j2sch,它让你的生活更简单。