This post is the 25th day article of Java Advent Calendar 2018. Although I am a beginner, I registered in the empty space for the time being with the spirit of "It is meaningful to participate!"
Despite the last day, I feel sorry for the thin content, but it seems like a beginner, so it's time for Hello World!
I heard a rumor from Java11 that java can be executed without compiling with javac, so I will try Hello World. However, there seems to be a limitation such as only when one source file is completed, so I will try that side as well.
OS:Windows 10 Java:Oracle Open JDK 11 Console: Command prompt
HelloJDK11.java
public class HelloJDK11 {
public static void main(String[] args) {
System.out.println("HelloWorld!");
}
}
Try to run
>java HelloJDK11.java
HelloWorld!
HelloJDK11.class was not created in the directory where HelloJDK11.java exists.
>javac HelloJDK11.java
>java HelloJDK11
HelloWorld!
As usual, HelloJDK11.class has been created.
>java HelloJDK11.java
error:Class found in application classpath: HelloJDK11
Apparently, if the class file exists on the classpath, executing the java file with the java command will result in an error. After deleting the class file, it can be executed again with java HelloJDK11.java.
public class HelloJDK11 {
public static void main(String[] args) {
System.out.println("HelloWorld!");
callMethod();
}
private static void callMethod() {
System.out.println("CalledMethod!");
}
}
Run
>java HelloJDK11.java
HelloWorld!
CalledMethod!
The method call was fine too.
HelloJDK11.java
public class SorryJDK11 {
public static void main(String[] args) {
~ Same as before, so omitted ~
}
javaファイル名:HelloJDK11.java Class name: SorryJDK11
Run and compile
>java HelloJDK11.java
HelloWorld!
CalledMethod!
>javac HelloJDK11.java
HelloJDK11.java:1:error:Class SorryJDK11 is public and file SorryJDK11.Must be declared in java
public class SorryJDK11 {
^
1 error
If the java file name and class name do not match,
HelloJDK11.java
public class HelloJDK11 {
public static void main(String[] args) {
Person suzuki = new Person("Suzuki");
System.out.println(suzuki.getName());
}
}
class Person {
//field
private String name;
//constructor
public Person(String name) {
this.name = name;
}
//Getter
public String getName() {
return this.name;
}
}
Run
> java HelloJDK11.java
Suzuki
There were no problems.
Divide HelloJDK11.java into two files, HelloJDK11.java and Person.java.
>java HelloJDK11.java
HelloJDK11.java:7:error:Can't find symbol
Person suzuki = new Person("Suzuki");
^
symbol:Class Person
place:Class HelloJDK11
HelloJDK11.java:7:error:Can't find symbol
Person suzuki = new Person("Suzuki");
^
symbol:Class Person
place:Class HelloJDK11
2 errors
error:Compilation failed
A compilation error has occurred!
According to "It must be completed in one source file." A compilation error has occurred.
>javac HelloJDK11.java
Two files, HelloJDK11.class and Person.class, were created and could be executed with the java command.
When I was training for newcomers, I remember that it was troublesome to javac and java every time, so I was interested in the operation that can be executed without javac in Java 11 this time and made an article.
According to JEP 330, the goal is not to allow Java to run without compilation like a scripting language.
The purpose is to execute a Java file as a command as a shebang (such as #! / Bin / sh on the first line of the shell). ./HelloJDK11 It seems that it can be executed like this. (I will try it next time.)
Recommended Posts