8 Replies Latest reply: Apr 16, 2010 12:00 PM by 801443 RSS

    Running exe file contained in the jar

    801443
      I am using an executable signed jar which has a .exe file and a .dll file as its contents. Now since my application relies on the .exe and .dll, i want to extract those files from the jar and use it in my application. But it seems that the .exe file after extracting from the jar has only read permissions (same for .dll). What do i need to do so that i can use the .exe file within my application after extracting from the jar.
        • 1. Re: Running exe file contained in the jar
          baftos
          I am doing the same thing without any problem on Win2k up to Windows 7.
          Where do you place the extracted files?
          I use System.getProperty("java.io.tmpdir").
          • 2. Re: Running exe file contained in the jar
            801443
            The issue is that I am running my program in a different directory and my jar reside in bin. When i launch my code using batch file then i need to first go to the directory where my bin folder is located and then extract the jar files. But it seems that even after executing the command I am in the same directory where i was before.

            For example say my current directory is C:\User\Admin\Desktop\Project\qa\abc\xyz then my current directory is xyz and i want to navigate to the C:\User\Admin\Desktop\Project\qa\bin where my jar is located and then extract executable and dll from the jar.

            What I have been trying till now is as follows:
                    String command[] = new String[4];
                     Process child;
                     
                     command[0] = "cmd";
                     command[1] = "cd";
                     command[2] = "..\\..\\bin";
                     command[3]= "";
                     try {
                           child = Runtime.getRuntime().exec(command);
                          String currentDir = new File(".").getCanonicalPath();
                          trace.out("ss", "Current dir is:" + currentDir);
                      } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                      }
                     
                     command[0] = "cmd";
                     command[1] = "jar";
                     command[2] = "xf";
                     command[3] = "foil.jar";
                     try {
                           child = Runtime.getRuntime().exec(command);
                      } catch (IOException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                      }
                 
            I am launching my application via command prompt(a batch file launches my application).
            • 3. Re: Running exe file contained in the jar
              801443
              Any views / suggestions?????
              • 4. Re: Running exe file contained in the jar
                807580
                roaan wrote:
                Any views / suggestions?????
                Take a look at the 4 sections of [http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html|http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html] and implement ALL the recommendations. Also, read the Javadoc for the overloaded Runtime.exec() methods.
                • 5. Re: Running exe file contained in the jar
                  801443
                  Hi,

                  Thanks for pointing me out to that great tutorial. But one thing still puzzles me is that i followed their advice. I can run Windows commands but in my case i want to run jar xf somejar. This does not seem to run as yet.

                  The code that I am using right now is:
                  import java.util.*;*
                  *import java.io.*;
                  class StreamGobbler extends Thread
                  {
                      InputStream is;
                      String type;
                  
                      StreamGobbler(InputStream is, String type)
                      {
                          this.is = is;
                          this.type = type;
                      }
                  
                      public void run()
                      {
                          try
                          {
                              InputStreamReader isr = new InputStreamReader(is);
                              BufferedReader br = new BufferedReader(isr);
                              String line=null;
                              while ( (line = br.readLine()) != null)
                                  System.out.println(type  +">"+  line);    
                              } catch (IOException ioe)
                                {
                                  ioe.printStackTrace();  
                                }
                      }
                  }
                  public class GoodWindowsExec
                  {
                      public static void main(String args[])
                      {
                          //if (args.length < 1)
                          //{
                          //    System.out.println("USAGE: java GoodWindowsExec <cmd>");
                          //    System.exit(1);
                          //}
                  
                          try
                          {            
                              String osName = System.getProperty("os.name" );
                                 System.out.println(osName);
                              String[] cmd = new String[3];
                              if( osName.equals( "Windows Vista" ) )
                              {
                                  cmd[0] = "cmd.exe" ;
                                  cmd[1] = "/C" ;
                                  cmd[2] = "dir ..\\..\\bin"; // this seems to work but if i substitute it for jar xf ..\\..\\bin\\somejar it does not work
                              }
                              else if( osName.equals( "Windows 95" ) )
                              {
                                  cmd[0] = "command.com" ;
                                  cmd[1] = "/C" ;
                                  cmd[2] = "cd ..\\..\\bin";
                              }
                  
                              Runtime rt = Runtime.getRuntime();
                              System.out.println("Execing "  +cmd[0]+  " "  +cmd[1]+ 
                   " "  +cmd[2]);+
                  +            Process proc = rt.exec(cmd);+
                  +            // any error message?+
                  +            StreamGobbler errorGobbler = new+ 
                  +                StreamGobbler(proc.getErrorStream(), "ERROR");+            
                  
                  +            // any output?+
                  +            StreamGobbler outputGobbler = new+ 
                  +                StreamGobbler(proc.getInputStream(), "OUTPUT");+
                  
                  +            // kick them off+
                  +            errorGobbler.start();+
                  +            outputGobbler.start();+
                  
                  +            // any error???+
                  +            int exitVal = proc.waitFor();+
                  +            System.out.println("ExitValue: "+  exitVal);        
                          } catch (Throwable t)
                            {
                              t.printStackTrace();
                            }
                      }
                  }
                  • 6. Re: Running exe file contained in the jar
                    801443
                    Okay I got that working by extracting the executable and then running it. But one question still puzzles me is that Is it possible to run .exe without even extracting it from the jar file?
                    • 7. Re: Running exe file contained in the jar
                      DrClap
                      roaan wrote:
                      Okay I got that working by extracting the executable and then running it. But one question still puzzles me is that Is it possible to run .exe without even extracting it from the jar file?
                      No.
                      • 8. Re: Running exe file contained in the jar
                        801443
                        Thanks a lot, after a little bit more searching i figured out that since the os does not know the contents inside the jar file, it makes sense to first extract it so that it can be run.