Is it somehow possible to have more than one source file with Java's "Single-file mode"?

60 views Asked by At

I'm aware that this question sounds a bit absurd, but it would be sooo nice to #include source code that is shared among multiple Java-single-file scripts, e.g. like so:

./scriptone.sh:

#!/usr/bin/java --source 11

public class Main {
    public static void main(String[] args) {
        System.out.println("Script #1");
        Util.printHello();
    }
}

#include util.java

scripttwo.sh:

#!/usr/bin/java --source 11

public class Main {
    public static void main(String[] args) {
        System.out.println("Script #2");
        Util.printHello();
    }
}

#include util.java

util.java:

public class Util {
    public static void printHello() {
        System.out.println("HELLO");
    }
}

And then:

$ ./scriptone.sh
Script #1
HELLO
$ ./scripttwo.sh
Script #2
HELLO
$

Instead, you get

$./scriptone.sh
.\scriptone.sh:10: error: illegal character: '#'
#include util.java
^
$

Unfortunately, Java's single-file mode does not support --source-path either, which would be a viable alternative to load and compile auxiliary classes.

Is there any "hack" to achieve what I want?

0

There are 0 answers