Java程序在cmd中执行而不使用设置路径或系统变量

发布于 2024-12-07 16:53:58 字数 1276 浏览 0 评论 0原文

我试图在命令提示符下执行一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

树深时见影 2024-12-14 16:53:58

请尝试这个:

C:\Program Files\Java\jdk1.6.0_24\bin>java -cp D:\My_Programs HelloWorld

甚至那个:

C:\anywhere> C:\Program Files\Java\jdk1.6.0_24\bin\java -cp D:\My_Programs HelloWorld

-cp 告诉 java 可执行文件在哪里查找类 HelloWorld。提供类似文件的参数 D:\My_Programms\HelloWorld ,其中 Java 假定纯包名+类名将不起作用。

Please try this one:

C:\Program Files\Java\jdk1.6.0_24\bin>java -cp D:\My_Programs HelloWorld

or even that one:

C:\anywhere> C:\Program Files\Java\jdk1.6.0_24\bin\java -cp D:\My_Programs HelloWorld

The -cp tells the java executable where to look for the class HelloWorld. Giving a file-like argument D:\My_Programms\HelloWorld where Java assumes a pure packagename+classname will not work.

财迷小姐 2024-12-14 16:53:58

由于运行 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 want javac and java to be in your path so you can execute them while you're in your program directory. And then you can execute the program using java HelloWorld

关于从前 2024-12-14 16:53:58

你可以尝试一下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.

一身仙ぐ女味 2024-12-14 16:53:58
java -cp D:\My_Programs HelloWorld

因为目录层次结构要从类层次结构来考虑。

java -cp D:\My_Programs HelloWorld

Because the directory hierarchy from the class hierarchy to be considered.

一城柳絮吹成雪 2024-12-14 16:53:58

您还可以尝试通过 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.

一指流沙 2024-12-14 16:53:58

在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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文