java 您可以从 IDE 输出的终端运行 .class 文件吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4034312/
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
Can you run a .class file from terminal that is outputted by an IDE
提问by Bobby
I am trying to run a file from command line. The file is a .class file and is apart of a larger project that I compiled in Netbeans. I navigated to the .class file and ran
我正在尝试从命令行运行文件。该文件是一个 .class 文件,是我在 Netbeans 中编译的一个更大项目的一部分。我导航到 .class 文件并运行
java MyFile
java我的文件
And I got:
我得到了:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
Caused by: java.lang.ClassNotFoundException: PersonTest.class
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: PersonTest.class. Program will exit
Whats up with that? (I should mention that i'm running ubuntu)
那是怎么回事?(我应该提到我正在运行 ubuntu)
回答by Tomas Narros
You need to check this useful link java - the Java application launcher:
您需要检查这个有用的链接java - Java 应用程序启动器:
By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used
默认情况下,第一个非选项参数是要调用的类的名称。应该使用完全限定的类名
So, you have to write the full qualified name of the class (this includes the package name).
因此,您必须编写类的完整限定名称(包括包名称)。
So, the right way to execute your command is this (from the root dir where your class files are stored):
因此,执行命令的正确方法是这样的(从存储类文件的根目录中):
> java my.package.MyFile
Also, make sure to include all the needed dependencies at the classpath (-cp) argument (check the referenced link).
此外,请确保在类路径 (-cp) 参数中包含所有需要的依赖项(检查引用的链接)。
UPDATE:to include a classpath setting example:
更新:包含类路径设置示例:
java -classpath C:\MyProject\classes;C:\MyProject\lib\utility.jar my.package.MyFile
With this, the java runtime will search for the classes at the C:\MyProject\classes directory, and at the C:\MyProject\lib\utility.jar JAR file. You'll need not only your class direct dependencies, but the dependencies needed by the referenced files (the whole tree).
这样,java 运行时将在 C:\MyProject\classes 目录和 C:\MyProject\lib\utility.jar JAR 文件中搜索类。您不仅需要您的类直接依赖项,还需要引用文件(整个树)所需的依赖项。
回答by KevinDTimm
The answer appears to be in this line:
答案似乎在这一行:
Exception in thread "main" java.lang.NoClassDefFoundError: PersonTest/class
It means you didn't type:
这意味着你没有输入:
java MyFile
as you said in your original post, you typed
正如您在原始帖子中所说,您输入
java PersonTest.class
you should have typed
你应该打字
java PersonTest
回答by Sriharshaa
Just consider this example
只考虑这个例子
say I already have a folder src and I wrote in my notepad
说我已经有一个文件夹 src 并且我在记事本中写了
package test.oye;
class testclass {
static public void main (String [] args)
{
int a=3;
System.out.println(a);
}
}
then what go to src folder and you ,yourself create a folder named test and inside it oye . Then put your .java file in it . Then cd src/test/oye
only(in Command prompt or terminal).From there itself
然后去 src 文件夹,你自己创建一个名为 test 的文件夹,里面是 oye 。然后将您的 .java 文件放入其中。然后cd src/test/oye
只有(在命令提示符或终端中)。从那里本身
javac testclass.java
cd src
java test.oye.testclass
This will work for sure.
这肯定会奏效。
If you don't want to put .java file there … then just compile your .java file and get the .class file . Now create the test folder and then oye inside it ….and put .class file inside it ….
如果您不想将 .java 文件放在那里……那么只需编译您的 .java 文件并获取 .class 文件。现在创建 test 文件夹,然后将 oye 放入其中……并将 .class 文件放入其中……
Now go back to src …and then type
现在回到 src ...然后输入
java test.oye.testclass;
回答by nino
according to terminal ide, android requires classes in DEX format when running them.
根据终端ide,android在运行它们时需要DEX格式的类。
Try:
尝试:
dx --dex --output=practice.jar practice.class
dx --dex --output=practice.jar practice.class
Then run using this:
然后使用这个运行:
java -jar practice.jar practice
java -jar 练习.jar 练习
回答by Andrzej Doyle
Unless your class is entirely standalone (i.e. only references java.lang
classes like String
), you'll need to add other classes/JARs to the classpath when you invoke Java.
除非您的类是完全独立的(即只引用java.lang
像 之类的类String
),否则您在调用 Java 时需要将其他类/JAR 添加到类路径中。
The NoClassDefFoundError
(which usually states the name of the class by the way, and always includes a stacktrace) indicates that an external class that was available when your class was compiled, is not available on the classpath at runtime.
该NoClassDefFoundError
(通常规定的方式之类的名字,始终包括堆栈跟踪)表示,这是可用的,当你的类被编译的外部类,是不是可以在运行时类路径。
EDITbased on update:
基于更新的编辑:
You're invoking your process incorrectly. You don't need to append the .class
suffix of the file - doing so makes Java look for a file class class
in a subpackage.
您错误地调用了您的流程。您不需要附加.class
文件的后缀 - 这样做会使 Javaclass
在子包中查找文件类。
(P.S. you said you ran java MyFile
. That's a lie, you actually ran java PersonTest.class
. If you'd noted that to start with, it would have made it much easier for people to answer the question!)
(PS 你说你跑了java MyFile
。那是谎言,你实际上跑了java PersonTest.class
。如果你一开始就注意到这一点,那么人们回答这个问题就会容易得多!)
回答by Mark Baijens
Yes you can, they are compiled by a java compiler. If you have the right version of the jvm (often other versions work aswell) than it can be run. The information about your error is not enough to tell what went wrong. Your probably in the wrong folder, mistyped the classname, used a class in your code that couldn't be found, etc.
是的,你可以,它们是由 java 编译器编译的。如果您拥有正确版本的 jvm(通常其他版本也可以),那么它就可以运行。关于您的错误的信息不足以说明哪里出了问题。您可能在错误的文件夹中,错误输入了类名,在您的代码中使用了无法找到的类等。