Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run exe which is packaged inside jar file

Tags:

java

jar

exe

I am executing an exe through my java program. The path is hardcoded in Java.

I have packaged my the exe in the jar.

But am stuck as I have the path name hardcoded in the Java file, so I am not able to execute my jar as a stand alone program.

Any hints for packaging such jar i.e having an exe inside and able to run it as a stand alone program?

like image 760
krisp Avatar asked Mar 01 '09 17:03

krisp


People also ask

Can you convert jar to exe?

Package your Java application as a jar, and Executor will turn the jar into a Windows .exe file, indistinguishable from a native application. Simply double-clicking the .exe file will invoke the Java Runtime Environment and launch your application. Do you have a link to Executor?

Is .exe same as jar?

An Exe file is an executable file that can be executed in Microsoft OS environment. Jar file is container of Java Class files, including other resources related to the project. Jar file can be executed only if Java run time environment.

How do I convert exe to jar?

In Eclipse you can do it simply as follows : Right click on your Java Project and select Export. Select Java -> Runnable JAR file -> Next. Select the Destination folder where you would like to save it and click Finish.


1 Answers

This will extract the .exe to a local file on the local disk. The file will be deleted when the Java program exists.

import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.security.CodeSource; import java.security.ProtectionDomain; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile;  public class Main {     public static void main(final String[] args)         throws URISyntaxException,                ZipException,                IOException     {         final URI uri;         final URI exe;          uri = getJarURI();         exe = getFile(uri, "Main.class");         System.out.println(exe);     }      private static URI getJarURI()         throws URISyntaxException     {         final ProtectionDomain domain;         final CodeSource       source;         final URL              url;         final URI              uri;          domain = Main.class.getProtectionDomain();         source = domain.getCodeSource();         url    = source.getLocation();         uri    = url.toURI();          return (uri);     }      private static URI getFile(final URI    where,                                final String fileName)         throws ZipException,                IOException     {         final File location;         final URI  fileURI;          location = new File(where);          // not in a JAR, just return the path on disk         if(location.isDirectory())         {             fileURI = URI.create(where.toString() + fileName);         }         else         {             final ZipFile zipFile;              zipFile = new ZipFile(location);              try             {                 fileURI = extract(zipFile, fileName);             }             finally             {                 zipFile.close();             }         }          return (fileURI);     }      private static URI extract(final ZipFile zipFile,                                final String  fileName)         throws IOException     {         final File         tempFile;         final ZipEntry     entry;         final InputStream  zipStream;         OutputStream       fileStream;          tempFile = File.createTempFile(fileName, Long.toString(System.currentTimeMillis()));         tempFile.deleteOnExit();         entry    = zipFile.getEntry(fileName);          if(entry == null)         {             throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());         }          zipStream  = zipFile.getInputStream(entry);         fileStream = null;          try         {             final byte[] buf;             int          i;              fileStream = new FileOutputStream(tempFile);             buf        = new byte[1024];             i          = 0;              while((i = zipStream.read(buf)) != -1)             {                 fileStream.write(buf, 0, i);             }         }         finally         {             close(zipStream);             close(fileStream);         }          return (tempFile.toURI());     }      private static void close(final Closeable stream)     {         if(stream != null)         {             try             {                 stream.close();             }             catch(final IOException ex)             {                 ex.printStackTrace();             }         }     } } 
like image 92
TofuBeer Avatar answered Sep 24 '22 06:09

TofuBeer