1

Disclaimer: The question has a similar solution and arguably duplicated from "How do I run a Java program from the command line on Windows?", but I still believe it can be helpful to those whom asked the same wrong questions like me.

My mistake, as it can be found in the comments, was to be expecting the compiler to spill out an extensionless executable file (like in C), but instead, I only saw a .class file being created, leading me to run mistakenly java filename.class

If that happens, just run java filename with no extension and the code will run correctly.


I'm a very beginner at java and don't have the option of using it in Linux, so I was following the tutorial from an algorithm course on Coursera from Princeton Uni, and I installed the jdk-11.0.2 from here: https://lift.cs.princeton.edu/java/windows/

Just to see myself getting a class file instead of an executable after running javac file.java

I searched around and couldn't find anything so far, so I hope someone here saw a similar issue at some point.

enter image description here

The code, just to avoid suspiciousness over it:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

NB: IntelliJ on Windows compiles and runs it perfectly through the following command:

"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:{PATH_TO_INTELLIJ}\lib\idea_rt.jar=64104:{PATH_TO_INTELLIJ}\bin" -Dfile.encoding=UTF-8 -classpath {PATH_TO_MY_PROJECT};{PATH_TO_MY_PROJECT}\.lift\stdlib.jar;{PATH_TO_MY_PROJECT}\.lift\introcs.jar HelloWorld

enter image description here

Marcos Defina
  • 128
  • 13
  • 2
    A java compiler produces compiled code (`.class`) files. To run it, you invoke it w/o the extension (i.e. `java HelloWorld`) – blurfus Feb 10 '21 at 20:29
  • No way... I was running `java HelloWorld.class` cause I didn't see any extensionless file haha.. running only `java HelloWorld` worked hahaha thanks – Marcos Defina Feb 10 '21 at 20:32
  • please add this as an answer and let me upvote and choose you as the answer guy haha – Marcos Defina Feb 10 '21 at 20:33
  • actually, it might be best to close this question as a duplicate... Feel free to delete the question if you feel like it https://stackoverflow.com/questions/16137713/how-do-i-run-a-java-program-from-the-command-line-on-windows – blurfus Feb 10 '21 at 20:40
  • @blurfus do you think so? I didn't get to that post easily after some minutes of searching, might be helpful for someone that got to the wrong direction as me. But fair to include the reference in the main body. If more people agree with you upvoting your comment I will delete. – Marcos Defina Feb 10 '21 at 20:45
  • 1
    I think so but it's your question so I'd leave it up to you as to what to do with it :) - have a good day mate! – blurfus Feb 10 '21 at 20:47

1 Answers1

1

A java compiler produces compiled code (.class) files.

To run it, you invoke it w/o the extension (i.e. java HelloWorld)

More details: https://beginnersbook.com/2013/05/first-java-program/

blurfus
  • 13,485
  • 8
  • 55
  • 61