简体   繁体   English

Java会编译我的程序,但是我无法运行它

[英]Java compiles my program, but I cannot run it

I want to run a Java file with the following source code: 我想使用以下源代码运行Java文件:

package u0a1;
public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }

}

To run the file I did the following things: C:\\.. \\u0\\u0a1> javac HelloWorld.java (this works, class file is created) Then I try to run it with: 要运行该文件,我做了以下操作: C:\\.. \\u0\\u0a1> javac HelloWorld.java (此方法有效,创建了类文件),然后尝试使用以下命令运行它:

C:\..\u0> java u0a1.HelloWorld 

This step doesn't work. 此步骤无效。 Main class could not be found. 找不到主类。

I also tried 我也试过

C:\..\u0\u0a1> java HelloWorld
C:\..\u0> java u0a1\HelloWorld 

None of them worked. 他们都没有工作。

This is a piece i found somewhere else, worked for me. 这是我在其他地方找到的作品,为我工作。

Have you set your JAVA_HOME correctly? 您是否正确设置了JAVA_HOME? If not you have to work with the full path 如果不是这样,您必须使用完整路径

Example: "C:\\Program Files\\Java\\jdk1.7.0_51\\bin\\javac.exe" HelloWorld.java 示例: "C:\\Program Files\\Java\\jdk1.7.0_51\\bin\\javac.exe" HelloWorld.java

If you have runtime issues, you should work it out like this 如果遇到运行时问题,应按以下方法解决

Select MAIN directory - not package directory 选择主目录-不是软件包目录

java u0a1/HelloWorld

If you have problems with CLASSPATH or JAVA_HOME - try this: "C:\\Program Files\\Java\\jdk1.7.0_51\\bin\\javac.exe" HelloWorld.java 如果您在CLASSPATH或JAVA_HOME上遇到问题,请尝试以下操作: "C:\\Program Files\\Java\\jdk1.7.0_51\\bin\\javac.exe" HelloWorld.java

source: http://quandano.com/questions/how-to-run-a-java-file-within-a-package-from-cmd 来源: http//quandano.com/questions/how-to-run-a-java-file-within-a-package-from-cmd

You are compiling a program of package , so it should compile this way 您正在编译package程序,因此应以这种方式编译

C:.. \\u0\\u0a1> javac -d . C:.. \\ u0 \\ u0a1> javac -d。 HelloWorld.java HelloWorld.java

here -d for creating package u0a1 -d用于创建软件包u0a1

and " . " for from current working directory 和“ ”代表当前工作目录

after compiling this way a folder will create with name "u0a1" 以这种方式编译后,将创建一个名为“ u0a1”的文件夹

then other thing will work properly 然后其他东西会正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM