Creating and using .exe in JAR

Go To StackoverFlow.com

0

I have some code written in Java that executes some .exe file. It first creates a temporary file from where it executes it and then destroys that file after the execution is done.

The only problem with this is, it requires the executable file to be in the same package as that of the class having main function. I want to place and access my .exe file from other locations as well because while creating the JAR file of my project it never executes that exe file.

Is there some other way by which my .exe file can also be a part of my JAR file irrespective of its location in my project?

Here's the code :

package com.web.frame;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
//import java.net.URL;

public class Test{
  public void Test1(String fileAddr,String filename, String destFilenam) throws       Exception {
    // Place .exe into the package folder. 
  InputStream src =this.getClass().getResourceAsStream("DECRYPT.exe");
if(src!=null) {
   File exeTempFile = File.createTempFile("dspdf", ".exe");
   byte []ba=new byte[src.available()];
   src.read(ba,0,ba.length);
 exeTempFile.deleteOnExit();  
   FileOutputStream os=new FileOutputStream(exeTempFile);
   os.write(ba,0,ba.length);
   os.close();
   String hello=exeTempFile.getParent();
   System.out.println("Current Directory Of file : "+hello);
   String hello1=exeTempFile.getName();
   System.out.println("Full Name Of File : "+hello1);
   int l=hello1.length();
    l=l-4;
   char[] carray=hello1.toCharArray();
   String s = new String(carray,0,l);
   System.out.println(s);
   String param="cmd /c cd "+hello+" && "+s+" d 23 11 23 "+fileAddr+"\\"+filename+" "+destFilenam;
   Runtime.getRuntime().exec(param);
   Runtime.getRuntime().exec("c:\\Program Files\\VideoLAN\\VLC\\vlc.exe "+hello+"\\"+destFilenam);
   Runtime.getRuntime().exec("cmd /c del"+hello+"\\"+destFilenam);
  }
else
   System.out.println("Executable not found");
  } 

}
2012-04-05 03:09
by user1220961
1) Use a ProcessBuilder 2) Read & implement all the recommendations of When Runtime.exec() won't 3) Note that the directory for a Process can be set. 4) Please find your shift key, and apply it to the 1st letter of each sentence and the word 'I'. 5) Please don't use silly 'made up' spellings like 'u' for 'you'. 6) "please help" Please ask a question - Andrew Thompson 2012-04-05 03:40


0

The maximum you can do is place your exe anywhere in your classpath. Ensure that the jar's manifest has a classpath element. Then you should be able to access the exe by saying Test.class.getResourceAsStream("")

So create a folder, put the exe into that folder and include the folder in your classpath.

2012-04-05 03:25
by sethu
Ads