从命令行运行 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12964604/
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 from command line
提问by none
I have been mostly using eclipse so far. Now I'm trying to run java from terminal but I have a problem with packages.
到目前为止,我主要使用 eclipse。现在我正在尝试从终端运行 java,但我遇到了包问题。
This is my Main.java
file:
这是我的Main.java
文件:
package main;
class Main {
public static void main(String[] args) {
System.out.println("it's working");
}
}
I compile this using javac Main.java
and then run with java Main
which gives me:
我使用编译它javac Main.java
然后运行java Main
它给了我:
java Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Main. Program will exit.
When I remove package Main
everything works fine. What am I missing?
当我删除package Main
一切正常。我错过了什么?
java -version
gives:
java -version
给出:
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)
回答by Marplesoft
You need to run the java command up one directory level and give it in the fully qualified package name, eg: java main.Main
您需要在一个目录级别上运行 java 命令并在完全限定的包名称中提供它,例如: java main.Main
See How the Java Launcher Finds User Classesto learn how this works.
请参阅Java Launcher 如何查找用户类以了解其工作原理。
回答by Reimeus
You can use this command:
你可以使用这个命令:
java main.Main
Make sure the main
(lowercase) package directory is on the classpath.
确保main
(小写)包目录在类路径上。
回答by Lews Therin
It is possible that your classpath is not set correctly. Since you gave your .java file a package it is unnamed no longer.
您的类路径可能设置不正确。由于您给了 .java 文件一个包,它不再是未命名的。
An example:
一个例子:
java -cp ./package1/ main.Main //from the current directory and
//if main package is contained in package1
You need to fully qualify the class name. For future reference if you want to run from the command line you must stop the indirection (for lack of a better term) at the package level. Say your class was in the package package1.package2.Main.javaI would run it like so java -cp /blah/blah package1.package2.Main
您需要完全限定类名。为了将来参考,如果您想从命令行运行,您必须在包级别停止间接(因为缺少更好的术语)。假设你的类在包package1.package2.Main.java 中,我会像这样运行它java -cp /blah/blah package1.package2.Main
回答by Mordechai
Compile
编译
Windows:javac main\Main.java
Mac:javac main/Main.java
视窗:javac main\Main.java
Mac:javac main/Main.java
Run
跑
java main.Main
java main.Main
回答by Tao Huang
If you add package Main
, then you must put your source file in folder Main/Main.java. After that you can compile. When you run the program, go to Main folder using "cd", then write java -cp Main.Main
See my question similiar to yours noclassdeffounderror
如果添加package Main
,则必须将源文件放在 Main/Main.java 文件夹中。之后就可以编译了。运行程序时,使用“cd”转到主文件夹,然后写java -cp Main.Main
See my question similiarto yours noclassdeffounderror
回答by raj
try this...
试试这个...
In window , you just compile the code as
在 window 中,您只需将代码编译为
javac - d . Main.java
javac -d 。主程序
then a package(folder) with the name you specified in your class is created (in your code, package with name "main" is created) in the same path where your program reside...
然后在您的程序所在的同一路径中创建一个具有您在类中指定名称的包(文件夹)(在您的代码中,创建了名称为“main”的包)...
Then you just run the program as java main.Main or java main/Main
然后您只需将程序作为 java main.Main 或 java main/Main 运行