如何从命令行执行 java .class

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1279542/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:44:55  来源:igfitidea点击:

How to execute a java .class from the command line

java

提问by joe

I have a compiled java class:

我有一个编译的java类:

Echo.class

回声类

public class Echo {
    public static void main (String arg) {

            System.out.println(arg);
    }
}

I cdto the directory and enter: java Echo "hello"

cd到目录并输入:java Echo "hello"

I get this error:

我收到此错误:

C:\Documents and Settings\joe\My Documents\projects\Misc\bin>java Echo "hello"
Exception in thread "main" java.lang.NoClassDefFoundError: Echo
Caused by: java.lang.ClassNotFoundException: Echo
        at java.net.URLClassLoader.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Echo.  Program will exit.

What is the simplest way to get my java code in a form that I can run from the command line as apposed to having to use Eclipse IDE?

以我可以从命令行运行的形式获取我的 java 代码的最简单方法是什么,而不必使用 Eclipse IDE?

采纳答案by OscarRyz

Try:

尝试:

java -cp . Echo "hello"


Assuming that you compiled with:

假设你编译:

javac Echo.java 

Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )

然后有可能“当前”目录不在您的类路径中(java 在其中查找 .class 定义)

If that's the case and listing the contents of your dir displays:

如果是这种情况并列出您的目录的内容显示:

Echo.java
Echo.class

Then any of this may work:

那么其中任何一个都可以工作:

java -cp . Echo "hello"

or

或者

SET CLASSPATH=%CLASSPATH;.  

java Echo "hello"

And later as Fredrikpoints out you'll get another error message like.

稍后,正如Fredrik指出的那样,您会收到另一条错误消息,例如。

Exception in thread "main" java.lang.NoSuchMethodError: main

线程“main”中的异常 java.lang.NoSuchMethodError: main

When that happens, go and read his answer :)

当这种情况发生时,去阅读他的回答:)

回答by Michael Borgwardt

You need to specify the classpath. This should do it:

您需要指定类路径。这应该这样做:

java -cp . Echo "hello"

This tells java to use .(the current directory) as its classpath, i.e. the place where it looks for classes. Note than when you use packages, the classpath has to contain the root directory, not the package subdirectories. e.g. if your class is my.package.Echoand the .class file is bin/my/package/Echo.class, the correct classpath directory is bin.

这告诉 java 使用.(当前目录)作为它的类路径,即它寻找类的地方。请注意,当您使用包时,类路径必须包含根目录,而不是包子目录。例如,如果您的类是my.package.Echo.class 文件bin/my/package/Echo.class,则正确的类路径目录是bin.

回答by Fredrik

You have no valid main method... The signature should be: public static void main(String[]args);

您没有有效的 main 方法...签名应该是: public static void main( String[]args);

Hence, in your case the code should look like this:

因此,在您的情况下,代码应如下所示:

public class Echo {
    public static void main (String[] arg) {

            System.out.println(arg[0]);
    }
}

Edit:Please note that Oscaris also right in that you are missing . in your classpath, you would run into the problem I solve after you have dealt with that error.

编辑:请注意,Oscar也是正确的,因为您缺少 . 在您的类路径中,您会在处理该错误后遇到我解决的问题。

回答by quanticle

First, have you compiled the class using the command line javac compiler? Second, it seems that your main method has an incorrect signature - it should be taking in an array of String objects, rather than just one:

首先,您是否使用命令行 javac 编译器编译了该类?其次,您的 main 方法似乎有一个不正确的签名 - 它应该接受一个 String 对象数组,而不仅仅是一个:

public static void main(String[] args){

Once you've changed your code to take in an array of String objects, then you need to make sure that you're printing an element of the array, rather than array itself:

更改代码以接收 String 对象数组后,您需要确保打印的是数组元素,而不是数组本身:

System.out.println(args[0])

If you want to print the whole list of command line arguments, you'd need to use a loop, e.g.

如果要打印整个命令行参数列表,则需要使用循环,例如

for(int i = 0; i < args.length; i++){
    System.out.print(args[i]);
}
System.out.println();

回答by Scott Izu

My situation was a little complicated. I had to do three steps since I was using a .dll in the resources directory, for JNI code. My files were

我的情况有点复杂。我必须执行三个步骤,因为我在资源目录中使用了一个 .dll,用于 JNI 代码。我的文件是

S:\Accessibility\tools\src\main\resources\dlls\HelloWorld.dll
S:\Accessibility\tools\src\test\java\com\accessibility\HelloWorld.class

My code contained the following line

我的代码包含以下行

System.load(HelloWorld.class.getResource("/dlls/HelloWorld.dll").getPath());

First, I had to move to the classpath directory

首先,我必须移动到类路径目录

cd /D "S:\Accessibility\tools\src\test\java"

Next, I had to change the classpath to point to the current directory so that my class would be loaded and I had to change the classpath to point to he resources directory so my dll would be loaded.

接下来,我必须将类路径更改为指向当前目录,以便加载我的类,并且我必须将类路径更改为指向资源目录,以便加载我的 dll。

set classpath=%classpath%;.;..\..\..\src\main\resources; 

Then, I had to run java using the classname.

然后,我不得不使用类名运行 java。

java com.accessibility.HelloWorld 

回答by Gerard Doets

If you have in your java source

如果你有你的java源代码

package mypackage;

and your class is hello.java with

你的班级是 hello.java

public class hello {

and in that hello.java you have

在那个 hello.java 你有

 public static void main(String[] args) {

Then (after compilation) changeDir (cd) to the directory where your hello.class is. Then

然后(编译后)将目录(cd)更改为 hello.class 所在的目录。然后

java -cp . mypackage.hello

Mind the current directory and the package name before the class name. It works for my on linux mint and i hope on the other os's also

注意当前目录和类名之前的包名。它适用于我在 linux mint 上,我希望在其他操作系统上也适用

Thanks Stack overflow for a wealth of info.

感谢堆栈溢出提供了丰富的信息。

回答by Ryan Lundy

With Java 11 you won't have to go through this rigmarole anymore!

使用 Java 11,您将不再需要经历这些繁琐的事情!

Instead, you can do this:

相反,您可以这样做:

> java MyApp.java

You don't have to compile beforehand, as it's all done in one step.

您不必事先编译,因为这一步完成。

You can get the Java 11 JDK here: JDK 11 GA Release

您可以在此处获取 Java 11 JDK:JDK 11 GA Release