Java程序在cmd中执行而不使用设置路径或系统变量
我试图在命令提示符下执行一个简单的 java 程序(“HelloWorld”),而不使用 set path 选项或设置系统变量。假设java程序位于D:\My_Programs中,java可执行文件位于C:\Program Files\Java\jdk1.6.0_24\bin中。这是我所做的编译: C:\Program Files\Java\jdk1.6.0_24\bin>javac D:\My_Programs\HelloWorld.java 它正在创建一个 .class 文件,但相同的执行策略会创建一个异常: C:\Program Files\Java\jdk1.6.0_24\bin>java D:\My_Programs\HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: D:\My_Programs\HelloW
orld
Caused by: java.lang.ClassNotFoundException: D:\My_Programs\HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: D:\My_Programs\HelloWorld. Program will exit.
有人可以建议如何执行此文件。 预先感谢您的帮助。
代码:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I'm trying to execute a simple java program ("HelloWorld") in command prompt without using the set path option or setting the system variable. Suppose the java program is in D:\My_Programs and the java executable files are in C:\Program Files\Java\jdk1.6.0_24\bin. Here's what I did to compile:
C:\Program Files\Java\jdk1.6.0_24\bin>javac D:\My_Programs\HelloWorld.java It is creating a .class file but the same strategy for execution creates an exception:
C:\Program Files\Java\jdk1.6.0_24\bin>java D:\My_Programs\HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: D:\My_Programs\HelloW
orld
Caused by: java.lang.ClassNotFoundException: D:\My_Programs\HelloWorld
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: D:\My_Programs\HelloWorld. Program will exit.
Can someone suggest on how to execute this file.
Thanks in advance for your help.
The code:
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请尝试这个:
甚至那个:
-cp
告诉java
可执行文件在哪里查找类HelloWorld
。提供类似文件的参数D:\My_Programms\HelloWorld
,其中 Java 假定纯包名+类名将不起作用。Please try this one:
or even that one:
The
-cp
tells thejava
executable where to look for the classHelloWorld
. Giving a file-like argumentD:\My_Programms\HelloWorld
where Java assumes a pure packagename+classname will not work.由于运行
javac
时您位于 Java 目录而不是程序目录中,因此类文件也可能在那里。这通常是一件坏事 - 您希望 javac 和 java 位于您的路径中,以便您可以在程序目录中执行它们。然后你可以使用java HelloWorld
执行程序Since you were in the Java directory rather than the directory of your program when you ran
javac
the class file is probably there as well. That's generally a bad thing - you wantjavac
andjava
to be in your path so you can execute them while you're in your program directory. And then you can execute the program usingjava HelloWorld
你可以尝试一下java -cp "D:\My_Programs" HelloWorld,前提是你编译的HelloWorld.java是一个主类。
You can try this way java -cp "D:\My_Programs" HelloWorld,the precondition is that HelloWorld.java you compiled is a main class.
因为目录层次结构要从类层次结构来考虑。
Because the directory hierarchy from the class hierarchy to be considered.
您还可以尝试通过 cd D:\My_Programs 更改 cmd 中的目录,然后执行 java HelloWorld。它将执行该文件。唯一的前提条件是类文件应该存在于该位置。
You can also try with changing your directory in cmd by cd D:\My_Programs and then execute java HelloWorld. it will execute the file. The only pre condition is that class file should be present at that location.
在cmd中运行.java文件之前,将文件重命名为类名才可以运行。例如,在本例中,将记事本文件保存为“HelloWorld.java”
Before running the .java file in cmd, rename the file to the class name only then will it work. For example in this case, save the notepad file as 'HelloWorld.java'