Curlystraw
Curlystraw

Reputation: 295

Processbuilder-opening java script in linux

Back again, this time with a java question. I was told how to get Processbuilder to run nonnative scripts (calling the program that would run the script), but I've been trying to run a java script and have run into a couple problems. First off, should I use a .class or .jar? both of these can be run but I'm not sure which one of them will work better. And then how do I execute them correctly? I've tried calling java (/usr/lib/jvm/java-6-openjdk/jre/bin/java) and then giving the filepath to the class file, but that doesn't seem to work.

Any ideas?

Upvotes: 0

Views: 481

Answers (2)

bmargulies
bmargulies

Reputation: 100113

You either need -jar and the pathname of a jar that has a manifest that names your main class, or -cp with the pathname of a directory that has your classes in it in the standard layout, or -cp with the pathname of a jar followed by the name of the class with a main.

java -jar I_AM_A_JAR_WITH_A_MANIFEST.jar

java -cp I_AM_JAR_1.jar:I_AM_JAR2.jar... this.is.my.FooClass

java -cp dir_path1:dir_path2:dir_path3 this.is.my.FooClass

where the 'dir_pathN' is a dir with standard class hierarchy.

Upvotes: 0

Ampp3
Ampp3

Reputation: 640

I agree with sarnold in terms of the .jar question. In terms of executing code using ProcessBuilder, you can execute a .jar file as long as this file contains a main() method, and has the Main-Class manifest header, which can be generated when the .jar is created. Once you have the .jar created, you'd use a command like this to run the .jar:

java -jar jar_file_name_here.jar

If you have multiple main classes and you want to run a specific one, you could use a command like this:

java -jar jar_name.jar com.main.class.package.path.here.SomeClassName

Are you trying to execute someone elses .jar, or is it one of your own that you just want to be executed inside a script? Why are you using a script, out of curiosity?

Upvotes: 1

Related Questions