Java 如何运行作为 cmd 包一部分的 .class 文件?

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

How to run a .class file that is part of a package from cmd?

javaclasscmdpackagejavac

提问by

I keep getting errors when I make my .classpart of a packageand try to run it from cmd.

当我成为.classa 的一部分package并尝试从 cmd 运行它时,我不断收到错误消息。

Here's the code that works after using javacand then java:

这是使用javac然后java后工作的代码:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

and then the code that does not work:

然后是不起作用的代码:

package com;

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

giving me this error after trying to run the program via the command: java HelloWorld:

尝试通过以下命令运行程序后给我这个错误java HelloWorld::

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: com/HelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access0(Unknown Source)
    at java.net.URLClassLoader.run(Unknown Source)
    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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Here's what I've tried doing so far:

这是我到目前为止尝试做的事情:

java -cp . HelloWorld
java -cp . com.HelloWorld
java -cp . com/HelloWorld
java HelloWorld
java com.HelloWorld
java com/HelloWorld

Keep in mind that javacreturns with no errors and that simply removing package com;solves the problem. Sometimes in other scenarios I get an error that says the main class file cannot be found or something along those lines.

请记住,javac返回没有错误,只需删除即可package com;解决问题。有时在其他情况下,我会收到一条错误消息,指出无法找到主类文件或类似内容。

What am I doing wrong?

我究竟做错了什么?

采纳答案by sgbj

Suppose you did cd C:/projectsand HelloWorld.classis in C:/projects/com, then just type:

假设你做了cd C:/projects并且HelloWorld.classC:/projects/com,那么只需输入:

java com.HelloWorld

回答by MadProgrammer

Packages are directly related to the expected directory location of the file.

包与文件的预期目录位置直接相关。

That is, if you have a source file with the package directive of com, it is expected that the file will live in the comdirectory.

也就是说,如果您有一个带有 package 指令的源文件com,则预计该文件将存在于该com目录中。

In your HelloWorldexample, it would be expected that the HelloWorld.javafile would be stored in the comdirectory, like com\HelloWorld.java

在您的HelloWorld示例中,预计HelloWorld.java文件将存储在com目录中,例如com\HelloWorld.java

When you compile the file, it will create a class file called HelloWorld.classin the comdirectory, like com\HelloWorld.class

当你编译这个文件时,它会创建一个HelloWorld.classcom目录中调用的类文件,比如com\HelloWorld.class

This way, when Java goes looking for the com.HelloWorldclass, it would actually be searching it's class path and appending com\HelloWorld.classto it until it finds your class file or runs out of class path

这样,当 Java 去寻找com.HelloWorld类时,它实际上会搜索它的类路径并附com\HelloWorld.class加到它,直到它找到你的类文件或用完类路径

Example

例子

So, I copied your HelloWorld.java(with package) example to C:\java\com\HelloWord.java

所以,我将您的HelloWorld.java(带包)示例复制到C:\java\com\HelloWord.java

From the command line, I changed to the C:\javadirectory...

从命令行,我切换到C:\java目录...

C:\java>dir com
 Volume in drive C is OS
 Volume Serial Number is ####-####

 Directory of C:\java\com

09/08/2013  01:55 PM    <DIR>          .
09/08/2013  01:55 PM    <DIR>          ..
09/08/2013  01:55 PM               135 HelloWorld.java

Then I compiled the HelloWorld.java

然后我编译了 HelloWorld.java

C:\java>javac com\HelloWorld.java

Then I ran it...

然后我跑了...

C:\java>java com.HelloWorld
Hello World!


You might like to have a read through Packagestutorial

您可能希望通读Packages教程

回答by Eldar

Try to use absolute directory or put your HelloWorld.class into ..\last_directory\com

尝试使用绝对目录或将您的 HelloWorld.class 放入 ..\last_directory\com

1. java -cp .......\last_directory com.HelloWorld
2. java -cp .......\last_directory HelloWorld(with created com)

回答by Ankur Shanbhag

Run the program from the parent directory of the comdirectory.

com目录的父目录运行程序。

java com.HelloWorld

回答by ihsan kocak

Create a folder named comunder Java folder and put the HelloWorld.javainto comfolder. Then run again javacand java.

com在 Java 文件夹下创建一个名为的文件夹并将其HelloWorld.java放入com文件夹。然后再次运行javacjava

回答by BilalDja

You should compile it first by typing this command in CMD, for exemple your file is in c:\ directory :

您应该首先通过在 CMD 中键入此命令来编译它,例如您的文件在 c:\ 目录中:

C:\com>javac HelloWorld.java

After that you can run the result by typing:

之后,您可以通过键入以下内容来运行结果:

c:\com>java HelloWorld

回答by kenorb

The syntax is:

语法是:

java -classpath /path/to/package-folder <packageName>.<MainClassName>

So you may try: java com.HelloWorldwhich would expect com/HelloWorld.classfile to be present as classpath by default points to the current directory (if not specified).

所以,你可以尝试:java com.HelloWorld它会期望com/HelloWorld.class文件存在的类路径默认指向当前目录(如果未指定)。

In case you're in different folder, try specifying classpath:

如果您在不同的文件夹中,请尝试指定classpath

$ CLASSPATH=/path/to/package-folder/ java com.HelloWorld
Hello World!
$ java -cp /path/to/package-folder/ com.HelloWorld
Hello World!
$ cd /path/to/package-folder/ && java com.HelloWorld
Hello World!

For further explanation, check: How do I run Java .class files?

如需进一步说明,请查看:如何运行 Java .class 文件?

回答by Pravin

You do not need a -cp flag while running a java class , -cp needed while running a class or a main program from a binary (jar) file. While running a main program from a command line you need to make sure you have the class in the same folder structure as package name in the java file, ex.

运行 java 类时不需要 -cp 标志,从二进制 (jar) 文件运行类或主程序时需要 -cp 标志。从命令行运行主程序时,您需要确保类在与 java 文件中的包名称相同的文件夹结构中,例如。

home/user/foo java com.test.className   

here classNmae class file and exist under home/user/foo/com/test

hope it helps.

希望能帮助到你。

回答by Krystian

There could be such a problem, when you are executing 'java' command directly from folder of your '*.class' files. This command should be executed from project 'parent' directory. All is well explained in the following article:

当您直接从“*.class”文件的文件夹中执行“java”命令时,可能会出现这样的问题。此命令应从项目“父”目录中执行。所有这些都在以下文章中得到了很好的解释:

http://javarevisited.blogspot.de/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html

http://javarevisited.blogspot.de/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html

回答by Robin

When you compile the java code, use -d, in your case, it would be

当您编译 java 代码时,请使用 -d,在您的情况下,它将是

javac -d . com.HelloWorld.java

After the above command, java compiler generate a folder named "com", under the "com" folder, you will see your HelloWorld.class

执行完上述命令后,java编译生成一个名为“com”的文件夹,在“com”文件夹下,你会看到你的HelloWorld.class

Then under the same folder as you run javac, run the following command

然后在运行 javac 的同一个文件夹下,运行以下命令

java com.HelloWorld