Running a .jar with code - java

I'm trying to run a jar file from code - in a "launcher". The launcher is an applet, which downloads the needed files.
But for some reason, it only works on some computers? I can't seem to make any link between the computers it doesn't work on. Below is my code:
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + "\\bin\\javaw", "-jar", data_jarToRun, data_authKey);
pb.directory(new File(directory));
try {
pb.start();
window.setStage("Launched!");
System.exit(0);
} catch (Exception e) {
e.printStackTrace();
window.setFailed();
window.setData("Failed to launch!");
}
Note that:
data_jarToRun equals "theJar.jar"
data_authKey is a String that needs to be included in "theJar" arguments.
directory is the folder that "theJar" is found in.

Related

How I can install application like .exe or dll file by java code

How to install silently application in windows by java code. I have downloaded the file from server but need to install also on a single click.
How can I achieve this.?
If I understood it correct you want to install a third application from your java application. All you can do is below (this is for exe.. not sure about dll, I do not think you can run them). That should run the installable exe. But it will install or not that depends upon how that software works.. an give it a try.. But this is not recommended
public static void main(String args[]) {
try {
Process proc = Runtime.getRuntime().exec("your installable exe");
proc.waitFor(); //Wait for it to finish
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Try the following code:
String command = "C:\\setup.exe";
Runtime.getRuntime().exec("cmd /c "+command);
read more.
To run batch file try:
Add this to your batch file:
#echo off
C:\Windows\notepad.exe yourpath\omt.txt
In your java program:
String filePath = "C:/yourbatpath.bat";
try {
Process p = Runtime.getRuntime().exec(filePath);
} catch (Exception e) {
e.printStackTrace();
}
read this to get in depth idea.

Best way to run a batch (shell / CMD) command in Java / Eclipse?

I have created the following code below in Eclipse / java which executes a batch file, which in turn should also execute once all my TestNG tests have executed but sometimes the bat file will execute and sometimes it dosnt do anything at all, any ideas?
#AfterSuite(alwaysRun = true)
public void executeBatFile() {
try {
List cmdAndArgs = Arrays.asList("cmd", "/c", "copyPasteImgs.bat");
File dir = new File(Paths.get(System.getProperty("user.dir") + "/..").toRealPath() + "\\");
ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
pb.directory(dir);
Process p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The batch files moves files from a local folder to a remote folder (When the batch file hasnt worked via eclipse or invoked via jenkins I have manually executed the batch file and it did its jobs, very weird...)
thanks for your help
Apache Commons CLI provides functionalities to handle using command line from java. You can also use the exit value to have an idea about what is going on with your command. For example:
String command = "dir";
CommandLine oCmdLine = CommandLine.parse(command);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
int iExitValue = oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
System.err.println("Execution failed.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("permission denied.");
e.printStackTrace();
}

Running a .exe file from a Java web app doesn't work

I'm trying to run a .exe file develop in Pascal from my web app(Windows + Primefaces 5 + Tomcat 8). The program generate a text file that I'm gonna read it after but it seems it doens't have the permission to do that, no exceptions were threw.
Here is how I pick the path:
String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/WEB-INF/lib/");
projeto.setQtde(1);
this.esquemas = gerenciarProjeto.realizarCorte(projeto, usuario, path+"/");
and here is how I call the program:
Runtime rt = Runtime.getRuntime();
Process pc = rt.exec(this.caminho+"cortebi.exe");
InputStreamReader isr = new InputStreamReader(pc.getErrorStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null){
System.out.println(line);
}
System.out.println("</ERROR>");
int exitVal = pc.waitFor();
System.out.println("Process exitValue: " + exitVal);
I realized that if I put the .exe file into my project root and run Eclipse as administrator it works. But I do not know how to put it into my web app root to do the same after it's deployed, I've tried to put it in diferent locations and nothing!
I was able to figure out what was the problem. I tought that Runtime.getRuntime().exec() would behave like a tradicional DOS prompt but I was wrong. In fact the problem itself was not a windows or file permission problem but a misconception of how the exec() method works. I wrote a new piece of code:
public void executarCortebi(File file){
try {
Process pc = Runtime.getRuntime().exec("cmd /c start cortebi.exe",null, file);
StreamGobbler error = new StreamGobbler(pc.getErrorStream(), "ERRO");
StreamGobbler output = new StreamGobbler(pc.getInputStream(), "OUTPUT");
error.start();
output.start();
pc.waitFor();
Thread.sleep(800);
} catch (IOException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
As I said the way the exec() method works is more restricted I found good material here:When Runtime.exec() won't.
Explaining the parameters of getRuntime.exec() used for me, we have the fallow: First the command itself that must be executed(It's noticeable the diferences between the old cold and the new one), second are the arguments for the .exe file wich in this case are none and third is the file that has the path from my .exe program.
Now everything is working!

Using ProcessBuilder to make a jar file isn't completing

I am trying to compile a jar file from within a Java program. When I run the code it starts to build the jar file and saves about 24k of it then the execution just seems to stop and wait (I am using Process.waitFor() and the program isn't finishing its execution). When I force the program to stop the size of the jar file jumps to about 45k. The jar should be about 1300k.
I tried creating a batch file and then calling the batch file with my ProcessBuilder but the same issue occurs. The batch file when run by itself works perfectly.
This is the code I have so far:
public static void buildJar() {
List<String> jarCmdArgs = new ArrayList<>();
jarCmdArgs.add("jar");
jarCmdArgs.add("cvfM");
jarCmdArgs.add(ROOT_DIRECTORY + File.separator + "my_jar.jar");
jarCmdArgs.add(".");
// jarCmdArgs.add("cmd.exe");
// jarCmdArgs.add("/C");
// jarCmdArgs.add(ROOT_DIRECTORY + File.separator + "make_jar.bat");
ProcessBuilder pb = new ProcessBuilder(jarCmdArgs);
pb.directory(new File(SRC_DIR_PATH));
try {
Process p = pb.start();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
This is the contents of the batch file:
//cd to my working dir with all the files to put into the jar
jar cvfM my_jar.jar .

.jar doesn't run external program

So i have a java project made in eclipse with sphinx voice recognition. If i say a certain word then it runs a .bat file.
if (resultText.equals("word")) {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("C:/c.bat");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In Eclipse it works fine, but after i export the .jar and run it, if i say that specific word, it doesn`t run that .bat. So any ideas why this only runs my .bat file from eclipse and not from command line? Thanks
I am not sure about this but atleast try this solution once.
Try giving the .bat file path as C:\\c.bat and then try again.
Try adding something like:
File f = new File("c:/c.bat");
if(f.exists()) {
// execute the file
Process process = runtime.exec(f.getAbsolutePath());
process.waitFor();
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();
// check the streams for errors
} else {
// log error
}
hth

Categories

Resources