简体   繁体   中英

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

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.

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.

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

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

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

Compilation:

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

Executing:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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