[Java 11] I tried to execute Java without compiling with javac

Introduction

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!

Run java without compiling! ??

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.

environment

OS:Windows 10 Java:Oracle Open JDK 11 Console: Command prompt

First of all, only ordinary Hello World

HelloJDK11.java

public class HelloJDK11 {
    public static void main(String[] args) {
        System.out.println("HelloWorld!");
    }
}

Try to run

>java HelloJDK11.java
HelloWorld!

Congratulations, I was able to run it without compiling.

HelloJDK11.class was not created in the directory where HelloJDK11.java exists.

Try compiling with javac

>javac HelloJDK11.java

>java HelloJDK11
HelloWorld!

As usual, HelloJDK11.class has been created.

However,,,

>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.

Make a method call just in case

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.

Make java file name and class name alias

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,

I can't compile it, but it seems that it can be executed as a java file.

Write a Person class and try new

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.

Split Person class into separate files

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!

Restrictions mentioned at the beginning

According to "It must be completed in one source file." A compilation error has occurred.

Once compiled with javac,
>javac HelloJDK11.java

Two files, HelloJDK11.class and Person.class, were created and could be executed with the java command.

Summary

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

[Java 11] I tried to execute Java without compiling with javac
I tried to interact with Java
I tried to make Basic authentication with Java
I tried to break a block with java (1)
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Stalin sort with Java Collector
I tried UDP communication with Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I tried to summarize Java lambda expressions
I tried to get started with WebAssembly
I tried using OpenCV with Java + Tomcat
I tried to make an Android application with MVC now (Java)
I tried to implement ModanShogi with Kinx
I tried to manage struts configuration with Coggle
I tried to manage login information with JMX
java I tried to break a simple block
I tried to implement deep learning in Java
I want to use java8 forEach with index
I tried to output multiplication table in Java
I tried to create Alexa skill in Java
I tried node-jt400 (execute)
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried what I wanted to try with Stream softly.
I tried to implement file upload with Spring MVC
I tried to implement Firebase push notification in Java
[Java] I tried to solve Paiza's B rank problem
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I started MySQL 5.7 with docker-compose and tried to connect
I tried to get started with Spring Data JPA
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried to draw animation with Blazor + canvas API
I tried OCR processing a PDF file with Java
I want to transition screens with kotlin and java!
[Java] I tried to implement Yahoo API product search
I tried passing Java Silver in 2 weeks without knowing Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I want to get along with Map [Java beginner]
I used to make nc (netcat) with JAVA normally
roman numerals (I tried to simplify it with hash)
I tried to find out what changed in Java 9
I tried to create a shopping site administrator function / screen with Java and Spring
When I tried to unit test with IntelliJ, I was told "java.lang.OutOfMemoryError: Java heap space"
[Swift] I tried to implement Instagram profile-like UI with UICollectionView only with code without storyboard
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried DI with Ruby
Java to play with Function
I tried Drools (Java, InputStream)
Connect to DB with Java
Connect to MySQL 8 with Java
I tried using Java REPL
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to verify yum-cron
I tried metaprogramming in Java
I tried to make an introduction to PHP + MySQL with Docker