I'd like to launch a jar file through an existing running Java application. I've looked into ProcessBuilder, Runtime and jproc and none of them work because they all say the same error:
CreateProcess error=193, %1 is not a valid Win32 application
The way I can get it to work is by adding the command: java -jar <path of network located jar> in any of the libraries above.
I don't want to do it this way because it messes up with location of paths and such that would run in the context of the network location if you "double clicked" the jar files directly.
Is there a way to run the jar on the network using the "default" JRE on Windows/Mac?
here is example code:
String networkLocation = "//appserver/testApp/test.jar";
// new ProcBuilder(networkLocation).withNoTimeout().run();
ProcessBuilder pb = new ProcessBuilder(networkLocation);
try
{
pb.start();
}
catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Found out how to make the working directory using jproc:
new ProcBuilder("java", "-jar", networkLocation).withNoTimeout().withWorkingDirectory(new File ("//appserver/testApp/")).run();
And also with Runtime
Runtime.getRuntime().exec(new String[]{"java","-jar", networkLocation}, null, new File("//appserver/testApp/"));
Related
I have a project that opens a browser with selenium. The structure looks something like this:
myProyect
src
com.open
OpenFirefox.java
lib
geckodriver
geckodriver.exe
If I make a jar file of this, I can only execute the jar if the jar in the same place as lib/geckodriver/geckodriver.exe. And both double click and java -jar firefox.jar from the console works.
Now, I need to execute this jar from another program. I have been opening other jars that have no dependencies with Desktop.getDesktop().open(file); with no problem, but when I try
try {
File file = new File("C:/Users/user/Desktop/firefox.jar");
Desktop.getDesktop().open(file);
} catch (Exception e) {
e.printStackTrace();
}
nothing happens, I dont even get an error.
What is happening?
You should use Runtime.exec()
Process process = Runtime.exec("java -jar " + filepath);
if (process.waitFor() == 0) {
System.out.println("process ran without errors");
}
I am trying to login to my JBoss server using Java and reload it.
I have tried following code:
ProcessBuilder processBuilder = new ProcessBuilder("C:\\jboss\\jboss-eap-6.4\\bin\\jboss-cli.bat -c --controller=10.10.10.10 --user=user --password=pass --command=:reload");
processBuilder.inheritIO();
Process p = processBuilder.start();
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
but I get this error:
CreateProcess error=2, The system cannot find the file specified
I have following settings for my environment variables-
1. JAVA_HOME- C:\Program Files\Java\jdk1.8.0_121
2. JBOSS_HOME-C:\jboss\jboss-eap-6.4
3. The bat file is present in the directory
4. I can login and reload it manually
I am out of ideas here.
I ended up using
Runtime.getRuntime().exec()
instead of
processBuilder()
Im trying to run a jar file from java code, But unfortunately does not success.
A few details about the jar file:
The jar file located in a different folder (For example - "Folder").
The jar file using a files and folders are in the root folder (the same "Folder" i mentioned above).
What im trying to do so far:
JAR file project.
In netbeans i checked that the main class are defiend (Project properties -> Run -> Main Class).
Other JAVA program
Trying to run with the command:
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar");
&&
Runtime.getRuntime().exec("javaw -jar "C:\\Software\\program.jar" "C:\\Software");
The jar file opened well, But he doesnt know and recognize his inner folders and files (the same "Folder" i mention above).
In short, it does not recognize its root folder.
Trying to run with ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "start", "javaw", "-jar", "C:\\Software\\program.jar");
pb.directory(new File("C:\\Software"));
try {
pb.start();
} catch (IOException ex) {
}
In Some PC's its works fine, But in other pc's its not work and i got an error message: "Could not find the main class"
** Offcourse if i run the jar with double click its works.
So how can i run a jar file from other java program ?
Use this variant of .exec where you specify working folder as the third argument. (In your examples, you always only use one argument.)
exec("javaw -jar "C:\\Software\\program.jar", null, "C:\\Software");
You can try to call it something like below. There are 2 types of calling it.
public class JarExecutor {
public static void main(String[] args) throws IOException, InterruptedException {
//This is first way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-jar" ,"C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar"});
//This is second way of calling.
Process proc=Runtime.getRuntime().exec(new String[]{"java","-cp","C:\\Users\\Leno\\Desktop\\JarsPractise\\JarsPrac.jar","com.shiva.practise.FloydTriangle"});
proc.waitFor();
BufferedInputStream is=new BufferedInputStream(proc.getInputStream());
byte[] byt=new byte[is.available()];
is.read(byt,0,byt.length);
System.out.println(new String(byt));
}
}
I'm trying to run a java application from a YARN application (in detail: from the ApplicationMaster in the YARN app). All examples I found are dealing with bash scripts that are ran.
My problem seems to be that I distribute the JAR file wrongly to the nodes in my cluster. I specify the JAR as local resource in the YARN client.
Path jarPath2 = new Path("/hdfs/yarn1/08_PrimeCalculator.jar");
jarPath2 = fs.makeQualified(jarPath2);
FileStatus jarStat2 = null;
try {
jarStat2 = fs.getFileStatus(jarPath2);
log.log(Level.INFO, "JAR path in HDFS is "+jarStat2.getPath());
} catch (IOException e) {
e.printStackTrace();
}
LocalResource packageResource = Records.newRecord(LocalResource.class);
packageResource.setResource(ConverterUtils.getYarnUrlFromPath(jarPath2));
packageResource.setSize(jarStat2.getLen());
packageResource.setTimestamp(jarStat2.getModificationTime());
packageResource.setType(LocalResourceType.ARCHIVE);
packageResource.setVisibility(LocalResourceVisibility.PUBLIC);
Map<String, LocalResource> res = new HashMap<String, LocalResource>();
res.put("package", packageResource);
So my JAR is supposed to be distributed to the ApplicationMaster and be unpacked since I specify the ResourceType to be an ARCHIVE. On the AM I try to call a class from the JAR like this:
String command = "java -cp './package/*' de.jofre.prime.PrimeCalculator";
The Hadoop logs tell me when running the application: "Could not find or load main class de.jofre.prime.PrimeCalculator". The class exists at exactly the path that is shown in the error message.
Any ideas what I am doing wrong here?
I found out how to start a java process from an ApplicationMaster. Infact, my problem was based on the command to start the process even if this is the officially documented way provided by the Apache Hadoop project.
What I did no was to specify the packageResource to be a file not an archive:
packageResource.setType(LocalResourceType.FILE);
Now the node manager does not extract the resource but leaves it as file. In my case as JAR.
To start the process I call:
java -jar primecalculator.jar
To start a JAR without specifying a main class in command line you have to specify the main class in the MANIFEST file (Manually or let maven do it for you).
To sum it up: I did NOT added the resource as archive but as file and I did not use the -cp command to add the syslink folder that is created by hadoop for the extracted archive folder. I simply startet the JAR via the -jar parameter and that's it.
Hope it helps you guys!
I am working in java for Ubuntu environment. I want to execute a script file but through java program. I have tried followings
try{
Runtime.getRuntime().exec("/usr/lib/flume-ng/bin/flume-ng agent -f /home/c4/Flume/New/ClientAgent.config -n clientAgent");
}
catch(Exception e){
e.printStackTrace();
}
And
ProcessBuilder pb = new ProcessBuilder("/usr/lib/flume-ng/bin/flume-ng", "agent",
"-f", "/home/c4/Flume/New/ClientAgent.config","-n","clientAgent");
try{
pb.start();
}
catch(Exception e){
e.printStackTrace();
}
But both of these don't start my process named: flume-ng, other are parameters passed to it. Java program compiles successfully but neither gives exception etc nor run process. Please guide me if i am doing something wrong.
Consider Apache Commons Exec
It provides reliable and cross-platform mechanism to launch external processes.
Here is the latest javadoc: http://commons.apache.org/proper/commons-exec/apidocs/index.html
And here is the simple tutorial: http://commons.apache.org/proper/commons-exec/tutorial.html