互联网上的大多数网站都说:
"use the 100 command to compile a 101 file. Then run it using the 102 command"
但今天我try 运行一个没有javac
的java程序,结果很奇怪.
以下是名为hello.java
的文件的内容:
public class Myclass {
public static void main(String[] args){
System.out.println("hello world");
}
}
然后我跑了起来:
$ javac hello.java
这给了我一个错误:
hello.java:1: error: class Myclass is public, should be declared in a file named Myclass.java
public class Myclass {
^
1 error
但当我在没有javac
命令的情况下运行它时,它执行时没有任何错误.
$ java hello.java
hello world
java
命令是否也编译该程序?如果是,为什么我们需要javac
命令?
my java的版本是:
openjdk version "12.0.2" 2019-07-16
OpenJDK Runtime Environment (build 12.0.2+10)
OpenJDK 64-Bit Server VM (build 12.0.2+10, mixed mode)