-2

Lets say that I have this line of code:

System.out.println("java ZipDirectory.java " + workspace);

This prints out something like

java ZipDirectory.java C:\Users\Dude\Desktop\Stuff

How do I get Java to actually execute the above, instead of just printing it out in console?

Edit: I've tried doing this so far and have yet to be able to get the functoin to run properly.

Runtime.exec("java ZipDirectory " + workspace);
&& 
Runtime.getRuntime().exec("java ZipDirectory " + workspace);

For added clarification since I don't really know a whole lot about Java, but here is the method within which I am working:

@Override
    public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
        System.out.println("java ZipDirectory.java " + workspace);
        Runtime.getRuntime().exec("java ZipDirectory " + workspace);
        run.addAction(new HelloWorldAction(name));
        //run.Action(new ZipDirectory.main(workspace))
        //ZipDirectory.main( workspace ) 
        listener.getLogger().println("Hello, " + name + "!");
        listener.getLogger().println("Workspace: " + workspace);
        
    }
  • do you want to get the console output from ZipDirectory java programm into the console from your current java programm? (which opens the ZipDirectory java programm) – verity Dec 18 '20 at 22:05
  • Don't need to get the console output from ZipDirectory. It should just zip whatever it finds in workspace. Unless I need the console output to know when the Zipping is complete? – The Traveling Coder Dec 18 '20 at 22:15

2 Answers2

1

To compile and run Java file you can use ProcessBuilder to execute commands

File javaFile = new File("...");
String javaFileName = javaFile.getName();
String javaClassName = javaFile.getName().replaceAll(".java", "");

Compile Java to Bytecode

ProcessBuilder compileProcess = new ProcessBuilder("javac", javaFileName);
compileProcess.directory(javaFileDirectory);
Process process1 = compileProcess.start();
process1.waitFor();

Run Java bytecode

ProcessBuilder executeProcess = new ProcessBuilder("java", javaClassName);
executeProcess.directory(javaFileDirectory);
Process process2 = executeProcess.start();
process2.waitFor()

You can also get the output from the execution and use it, I used this code in my IDE Project in JavaFX you can find the full code here with code that read from the execution

https://github.com/amrdeveloper/astro

Another idea if you will use the same class every time you can use Reflection API and can any method you want from the ZipDirectory file, you can add args and parameters

Class aClass = Class.forName("...");
Object instance = aClass.newInstance();
Method method = aClass.getMethod("functionName");
method.invoke(instance);
AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
0

Ok after your answer I now refer to your question:

  1. You can execute something like with the windows-cmd with Runtime.exec() and the parameter was what you wrote correctly above, also eg Runtime.exec(java -jar ZipDirectory.jar)

  2. You just have to be careful that it is in the folder in which the ZipDirectory.jar is located otherwise you could set the full path as parameter like this Runtime.exec(jar -jar C: //.../ZipDirectory.jar)

  3. To see whether zipping is already finished, you could test whether the newly created .zip file is already in the target folder.

verity
  • 375
  • 1
  • 11