How to run a simple java class on the command prompt in windows 8

 ̄綄美尐妖づ 提交于 2019-12-13 05:28:34

问题


I'm having a bit of trouble trying to run a java class on the command prompt. Its a very simple class and a friend of mine says it could be a problem with windows 8. Are there any suggestions. Here i'll show you the class and how I tried to compile it. I works fine in eclipse.

           package gmit;

           public class A {
             public static void main(String[] args) {
               System.out.println("hello");
             }
           }

In the command prompt I wrote C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>

followed by - javac A.java

java A.class

I tested the class using type A.java
and the text from the class does come up. What am I doing wrong? Any help would be great.


回答1:


If it runs in an Eclipse project, but not from command line, you could try this:

C:\Users\eclipse\workspace\Oct1stcasting\bin\gmit>java A

Inside a project directory, Eclipse saves source codes in /src and bytecodes in /bin. Thus, if you simply want to run your bytecode from command line, changing directory to /bin might suffice.




回答2:


To compile

C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>javac A.java

To run

C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>java A

Don't append the .class extension while running the java program




回答3:


Don’t add a .class suffix when using the java command.

Use java A or java -cp . A. The latter is required if the current directory is not implicitly used as class path.




回答4:


Compilation:

C:\Users\eclipse\workspace\Oct1stcasting\src>javac gmit/A.java

Executing:

C:\Users\eclipse\workspace\Oct1stcasting\src>java gmit/A


来源:https://stackoverflow.com/questions/19114617/how-to-run-a-simple-java-class-on-the-command-prompt-in-windows-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!