从命令行 Linux 运行 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3692229/
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 Program from Command Line Linux
提问by Altober
I am not very experience with java and this is driving me crazy. I wrote a java program FileManagementand I need to run it from the command line.
我对 Java 不是很有经验,这让我发疯。我写了一个java程序FileManagement,我需要从命令行运行它。
I can compile it from the command line with javac FileManagement/*.javawhich will create all the classes in that folder but when I try java FileManagement.Mainit says :
我可以从命令行编译它javac FileManagement/*.java,它将在该文件夹中创建所有类,但是当我尝试时java FileManagement.Main它说:
Exception in thread "main" java.lang.NoClassDefFoundError: FileManagement/Main
线程“main”中的异常 java.lang.NoClassDefFoundError: FileManagement/Main
The thing is that I have tried this same procedure in a remote computer and it is working fine. It is not working on mine.
问题是我在远程计算机上尝试了相同的过程,并且运行良好。它不适用于我的。
采纳答案by Bart Kiers
If your Mainclass is in a package called FileManagement, then try:
如果您的Main课程在名为 的包中FileManagement,请尝试:
java -cp . FileManagement.Main
in the parent folder of the FileManagementfolder.
在文件夹的父文件FileManagement夹中。
If your Mainclass is not in a package (the default package) then cdto the FileManagementfolder and try:
如果您的Main课程不在包(默认包)中,则cd到该FileManagement文件夹并尝试:
java -cp . Main
More info about the CLASSPATH and how the JRE find classes:
有关 CLASSPATH 以及 JRE 如何查找类的更多信息:
回答by Thomas Mueller
What is the package name of your class? If there is no package name, then most likely the solution is:
你的类的包名是什么?如果没有包名,那么最有可能的解决方案是:
java -cp FileManagement Main
回答by user001
Guys let's understand the syntax of it.
伙计们让我们了解它的语法。
If class file is present in the Current Dir.
java -cp . fileName
If class file is present within the Dir. Go to the Parent Dir and enter below cmd.
java -cp . dir1.dir2.dir3.fileName
If there is a dependency on external jars then,
java -cp .:./jarName1:./jarName2 fileName
Hope this helps.
如果当前目录中存在类文件。
爪哇 -cp 。文档名称
如果目录中存在类文件。转到父目录并在下面输入 cmd。
爪哇 -cp 。dir1.dir2.dir3.fileName
如果依赖于外部罐子,那么
java -cp .:./jarName1:./jarName2 文件名
希望这可以帮助。
回答by knocte
(This is the KISS answer.)
(这是 KISS 的回答。)
Let's say you have several .java files in the current directory:
假设您在当前目录中有几个 .java 文件:
$ ls -1 *.java
javaFileName1.java
javaFileName2.java
Let's say each of them have a main()method (so they are programs, not libs), then to compile them do:
假设它们每个都有一个main()方法(所以它们是程序,而不是库),然后编译它们:
javac *.java -d .
This will generate as many subfolders as "packages" the .java files are associated to. In my case all java files where inside under the same package name packageName, so only one folder was generated with that name, so to execute each of them:
这将生成与 .java 文件关联的“包”一样多的子文件夹。在我的情况下,所有 java 文件都在同一个包名下packageName,因此只生成了一个具有该名称的文件夹,因此要执行每个文件:
java -cp . packageName.javaFileName1
java -cp . packageName.javaFileName2

