Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java File runs with java FileName.java command but doesn't compile with javac Filename.java

Tags:

java

File name: B.java

public class B
{
    public static void main(String[] args){}
}
public class A{}
  • java B.java runs without error
  • javac B.java gives a compile error: class A needs to be declared in a file A.java

I understand that a java file can not have more than one public class, but why can a java file run without error using the java command when you get compile errors for the code using javac?

like image 679
errandlearn Avatar asked Aug 12 '20 19:08

errandlearn


People also ask

What is difference between javac and Java command?

The javac command is used to compile Java programs, it takes . java file as input and produces bytecode. Following is the syntax of this command. The java command is used to execute the bytecode of java.

How do I compile and run Java in javac?

Type 'javac MyFirstJavaProgram. java' and press enter to compile your code. If there are no errors in your code, the command prompt will take you to the next line (Assumption: The path variable is set). Now, type ' java MyFirstJavaProgram ' to run your program.

What is javac filename Java?

Description. The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.

Why is my Java program not compiling?

It means that javac.exe executable file, which exists in bin directory of JDK installation folder is not added to PATH environment variable. You need to add JAVA_HOME/bin folder in your machine's PATH to solve this error. You cannot compile and run Java program until your add Java into your system's PATH variable.


2 Answers

Java-11+ allows launching Single-File Source-Code programs without compiling. You can learn more about it from this article. As per the specification, if the first class in the file has main, it executes the same without caring for other public classes in the file. Thus, if you change the order of classes in the file and try java B.java, it will fail.

like image 194
Arvind Kumar Avinash Avatar answered Nov 14 '22 22:11

Arvind Kumar Avinash


My guess, and it is only a guess since I haven't used Java 11 extensively, is that:

  • The "javac" command produces class files that can be combined into a large program, and thus there could be multiple references to class A "elsewhere" in the program. It is this generality that produces the restriction on one public class per source file (though frankly I don't see why compiling B.java cannot result in A.class and B.class)

  • The "java" command processes a single source file. From that point of view it is irrelevant whether A is public or not; A can be used from within the single file (same package!) in any case.

like image 38
user13784117 Avatar answered Nov 14 '22 21:11

user13784117