0

I have a pile of .java files. They all have the same class name public MyClass. They all have a main method. They all may or may not have a package declaration at top, and I do not know ahead of time.

I am trying to write a script to compile and run these java programs. This is easy for the files without the package declaration... I just do some cp operations to setup, javac MyClass.java and java MyClass, then rm to teardown. However, the files with the package declaration require special attention. I have a few options that occur to me, including deleting the package lines, or attempting to read the package lines so that I know what the resulting directory structure should be. Both of these require me to go parsing through the .java files, which makes me sad.

Is there a way to compile and run these files without having to parse the .java files? Something like:

javac --ignore_package_structure MyClass.java 

would be ideal, but a quick look at the javac man pages suggests that such a thing doesn't exist.

Him
  • 5,257
  • 3
  • 26
  • 83
  • 1
    You're saying that all your files are named `MyClass.java`? The public class name must be identical with the file name (without the `.java`). – ublec Apr 28 '21 at 00:30
  • Yes, they are all named `MyClass.java`. What I mean here is that I have many different java programs (these are student submissions). I would like to compile and run them all separately. – Him Apr 28 '21 at 00:34
  • 2
    `(these are student submissions)` when you run a `MyClass` file, how do you decide which student gets credit for the result? – markspace Apr 28 '21 at 00:49
  • @ublec Only if the class is `public`, and it doesn't have to be, to be executed. – Andreas Apr 28 '21 at 01:55
  • @markspace the files are organized, so I know which file belongs to whom... – Him Apr 28 '21 at 04:37
  • Can we know how the files are organized then? It might bear on the problem of how to execute them. – markspace Apr 28 '21 at 13:13
  • I suppose I can organize them however. Currently I'm just putting them in `[student_identifier]/MyClass.java` – Him Apr 28 '21 at 14:23

2 Answers2

3

If we can assume that each student submits a single source file named HelloWorld.java, then we can use the "Launch Single-File Source-Code Programs" feature added by JEP 330 in Java 11:

java HelloWorld.java

We don't run javac first, we don't get a .class file (no cleanup needed), and any package declaration is handled automatically.

Remember, the students are still allowed to use many classes, they just all have to be submitted to you in a single source file.

The name of the class doesn't even matter. The first class in the source file is executed.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

There isn't any easy way to do this. You could use regex though, and replace all imports with this simple java regex:

"package \w+;"g

Simply stated, you create a Java program to replace all the package names. How to replace files: Find and replace words/lines in a file

ublec
  • 172
  • 2
  • 15