Home > Software engineering >  Difference between using javac<file> and java<file>
Difference between using javac<file> and java<file>

Time:03-20

From some experienced java coders I want to ask what is difference between using

javac <filename>
java <file_name_without_extention>

And

java <filename>

CodePudding user response:

They're equivalent if your source code is only a single file. The former (with the two commands) is the general way to compile and run Java source code, and it's still the correct way to compile larger projects. The latter is a new feature added in JDK 11 to make it easier to run individual files and very small programs.

From the proposal that suggested the feature

In source-file mode, the effect is as if the source file is compiled into memory, and the first class found in the source file is executed. For example, if a file called HelloWorld.java contains a class called hello.World, then the command

java HelloWorld.java

is informally equivalent to

javac -d <memory> HelloWorld.java
java -cp <memory> hello.World

CodePudding user response:


javac <Filename> - a java command that compiles java source files into bytecodes. 

It needs an extension because you are compiling the source file.


java -cp <classpath> <Classname> - a java command that executes the compiled bytecodes. 

It does not need an extension because you are merely telling it to search for the Class and its main() signature from the classpath to execute.

  • Related