-1

On Command Prompt I see the following

C:\Users\lek-h\Downloads\OOP>javac Example.java

C:\Users\lek-h\Downloads\OOP>

(i.e., no response from Command Prompt)

Java file

Under the following C:\Users\lek-h\Downloads\OOP\Example.java I have

class Example {
    // A Java program begins with a call to main().
    public static void main(String args[]) {
        System.out.println("Java drives the Web.");
    }
}

I have jdk 17 installed and set as path in system.

Under my system variables, I have System variable JAVA_HOME with value C:\Program Files\Java\jdk-17 System variable JAVA_PATH with value C:\Program Files\Java\jdk-17

Under user variables I have Variable Path with one value being %JAVA_HOME%\bin

  • 3
    That's exactly what you should see if your code compiles successfully. Congratulations! There should be a file `Example.class` in your current directory. – tgdavies Oct 06 '21 at 03:22
  • 1
    Does this answer your question? [How do I run a Java program from the command line on Windows?](https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows) – Zahid Khan Oct 06 '21 at 03:28

2 Answers2

1

You need to run two commands for running the java file.

  1. javac <filename.java>
  2. java < filename >

Run these commands on your command prompt:-

1) javac Example.java
2) java Example

Run these commands and check the output.

Harsh Mendapara
  • 303
  • 2
  • 10
  • 1
    Thanks for the response Harsh! I've tried the following and have not elicited any response. No Example.class was created, the println response was not shown in comand line either `1a. javac Example.java` `1b. java Example.java` `2a. javac Example` `2b. java Example` – anonymous_unagi Oct 07 '21 at 08:39
  • Try it once again. " javac filename.java" then type, " java filename". Or else check your path configuration. – Harsh Mendapara Oct 08 '21 at 05:26
  • 1
    `You need to run two commands for the java file compilation.` - that's not accurate. First command will do `compilation`, second will start JVM with given arguments – rkosegi Oct 08 '21 at 05:26
  • Yes, you are right. The second command will start the java virtual machine. – Harsh Mendapara Oct 08 '21 at 05:29
0

You can directly run the program using the java command.

java Example.java
Unmitigated
  • 76,500
  • 11
  • 62
  • 80