Search code examples
javajava-11single-source

In Java 11+ can we somehow excute without compilation for multiple files?


I was looking into Java single source file execution without any compilation. So I created this file, and it worked perfectly:

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;


public class J11SingleSource {
    public static void main(String[] args) {
        // Creating a list of hobbies
        List<String> hobbies = new ArrayList<>();
        hobbies.add("Reading");
        hobbies.add("Swimming");
        hobbies.add("Gardening");

        // Creating a Person object
        Person person = new Person("John", 30, hobbies);

        // Serialize the Person object to JSON
        Gson gson = new Gson();
        String json = gson.toJson(person);
        System.out.println("Serialized JSON: " + json);

        // Deserialize JSON to a Person object
        Person deserializedPerson = gson.fromJson(json, Person.class);
        System.out.println("Deserialized Person: " + deserializedPerson.getName());
        System.out.println("Deserialized Person's Age: " + deserializedPerson.getAge());
        System.out.println("Deserialized Person's Hobbies: " + deserializedPerson.getHobbies());
    }
    
    

    static class Person {
        private String name;
        private int age;
        private List<String> hobbies;

        // Constructor, getters, and setters
        public Person(String name, int age, List<String> hobbies) {
            this.name = name;
            this.age = age;
            this.hobbies = hobbies;
        }

        // Getters and setters
        ...
    }

    
}

And I ran it like this: java -cp lib/gson-2.8.8.jar J11SingleSource.java

And it gives me expected output:

Serialized JSON: {"name":"John","age":30,"hobbies":["Reading","Swimming","Gardening"]}
Deserialized Person: John
Deserialized Person's Age: 30
Deserialized Person's Hobbies: [Reading, Swimming, Gardening]

But I wanted to have a bit more of it. So I divided them into files:

First I extracted the Person class:

package tem.meaw.mua;

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;


public class Person {
        private String name;
        private int age;
        private List<String> hobbies;

        // Constructor, getters, and setters
        public Person(String name, int age, List<String> hobbies) {
            this.name = name;
            this.age = age;
            this.hobbies = hobbies;
        }

        // Getters and setters
        ...
 }

And the File containing main class:

package tem.meaw.mua;

import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

import tem.meaw.mua.Person;

public class Tem {
    public static void main(String[] args) {
        // Creating a list of hobbies
        List<String> hobbies = new ArrayList<>();
        hobbies.add("Reading");
        hobbies.add("Swimming");
        hobbies.add("Gardening");

        // Creating a Person object
        Person person = new Person("John", 30, hobbies);

        // Serialize the Person object to JSON
        Gson gson = new Gson();
        String json = gson.toJson(person);
        System.out.println("Serialized JSON: " + json);

        // Deserialize JSON to a Person object
        Person deserializedPerson = gson.fromJson(json, Person.class);
        System.out.println("Deserialized Person: " + deserializedPerson.getName());
        System.out.println("Deserialized Person's Age: " + deserializedPerson.getAge());
        System.out.println("Deserialized Person's Hobbies: " + deserializedPerson.getHobbies());
    }
}

And I'm trying to run it like this: java -cp lib/gson-2.8.8.jar Tem.java Person.java But it tells me that:

Tem.java:7: error: cannot find symbol
import tem.meaw.mua.Person;
                   ^
  symbol:   class Person
  location: package tem.meaw.mua
Tem.java:18: error: cannot find symbol
        Person person = new Person("John", 30, hobbies);
...

So in Java 11+, can we somehow run along with the dependant files into one file without compilation?


Solution

  • The feature to run a program consisting of multiple source files without compiling it before was first added to Java with version 22.

    Refer to JEP 458: Launch Multi-File Source-Code Programs and Using Source-File Mode to Launch Source-Code Programs.

    Previous versions of Java (like Java 11) do support only a single file: JEP 330: Launch Single-File Source-Code Programs.