Why after in the console after compiling java file. 1) The javac Helloworld.java command compiles everything well. There was a file with the extension .class (byte code) 2) I want to run the program on the logic of things I need to run java Helloworld.class, but it requires java Helloworld from me. Why is that? and when you run this command, what files does it pick up?

2 answers 2

Because the java program expects to receive as its parameter the name of the class, and not the file in which the bytecode of this class is located. If you use Java 11, you can run your program immediately with java Helloworld.java , that is, without first compiling it into a class file. In this Helloworld.class , Helloworld.class , in the same directory, should not be, otherwise it will not start.

    The answer to the second part of the question is “what files does it pick up” (it’s not very clear, however, what it picks up). The virtual machine ( java.exe ) searches for a class whose name is passed in a parameter in the current directory and in the directories listed in the -jar command line -jar , in the CLASSPATH system variable, and / or in the directories listed in the -cp parameter ( -classpath ). For example

      java -cp ./bin com.my.package.MyClass java -jar myJar.jar 

    This implies that the directory /bin has a subdirectory com/my/package , which contains the compiled file MyClass.class , and in the file myJar.jar there is a subdirectory named META-INF , which contains a file called MANIFEST.FM , in which there is a string like

     Main-Class: com.my.package.MyClass 

    (This is an indication to the virtual machine which class from the archive should be run) and in the com/my/package directory lies the class itself, MyClass.class , containing the entry point ( public static void main(String[] args) { ... } ). For details, see https://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html (however, it is not very clearly explained)