exe within a jar file

B

bkennedy_pu

I want to package and run an exe within an executable jar file. I've
tried using the getClass().getResource("relative/exe/location"), but
receive the following error when running the jar

java.io.IOException: CreateProcess:
jar:file:\E:\test\TestProgram.jar!\myexe.exe error=2

this occurs when I run the following code:

Runtime rt = Runtime.getRuntime();
Process proc =
rt.exec(this.getClass().getResource("/myexe.exe").toString());

I would appreciate any help or suggestions on the subject. We've
considered extracting it an running it from a temp location and
cleaning up the directory, but we would like to avoid this. Thanks!
 
M

Mike Schilling

bkennedy_pu said:
I want to package and run an exe within an executable jar file. I've
tried using the getClass().getResource("relative/exe/location"), but
receive the following error when running the jar

java.io.IOException: CreateProcess:
jar:file:\E:\test\TestProgram.jar!\myexe.exe error=2

this occurs when I run the following code:

Runtime rt = Runtime.getRuntime();
Process proc =
rt.exec(this.getClass().getResource("/myexe.exe").toString());

I would appreciate any help or suggestions on the subject. We've
considered extracting it an running it from a temp location and
cleaning up the directory, but we would like to avoid this. Thanks!

You haven't said which OS you're using, but in all the ones I'm familiar
with, the argument to Runtime.exec has to be something the native OS can
run, i.e. a file, not a entry in a zip file. You'll need to extract it.
 
A

Andrew Thompson

bkennedy_pu said:
I want to package and run an exe within an executable jar file. I've
tried using the getClass().getResource("relative/exe/location"), but
receive the following error when running the jar

java.io.IOException: CreateProcess:
jar:file:\E:\test\TestProgram.jar!\myexe.exe error=2

this occurs when I run the following code:

Runtime rt = Runtime.getRuntime();
Process proc =
rt.exec(this.getClass().getResource("/myexe.exe").toString());

It is not surprising that Win. cannot launch the .exe while
it is compressed. That is a luxury that we Java programmers
have come to expect, but applies mostly to Java classes.
I would appreciate any help or suggestions on the subject. We've
considered extracting it an running it from a temp location and
cleaning up the directory, but we would like to avoid this.

...well that was exactly what I was going to suggest,
since I feel it would be the best solution. Why do you
want to avoid that?

Andrew T.
 
M

Mike Schilling

Andrew Thompson said:
It is not surprising that Win. cannot launch the .exe while
it is compressed. That is a luxury that we Java programmers
have come to expect, but applies mostly to Java classes.


..well that was exactly what I was going to suggest,
since I feel it would be the best solution. Why do you
want to avoid that?

Think of all the issues it creates:

* The need to find a writeable temp directory
* The need to find a unique name to extract it to
* The problem of what to do if there isn't sufficient free space
* The impossibility of always deleting ithe executable afterward

None of these is insuperable, but it would be easier not to deal with them,
if that were possible (which it isn't.)
 
B

bkennedy_pu

I am running a windows environment, but the reasons mike brought up are
exactly the reasons why we don't want to extract the files.
 
M

Morten Alver

Mike said:
Think of all the issues it creates:

* The need to find a writeable temp directory
* The need to find a unique name to extract it to

You can do it like this (from
http://javaalmanac.com/egs/java.io/CreateTempFile.html):
File temp = File.createTempFile("pattern", ".suffix");

.... which finds the proper directory for you, and makes up a unique name.
* The impossibility of always deleting ithe executable afterward

According to the page linked above you can use:

// Delete temp file when program exits.
temp.deleteOnExit();

.... but I'm not sure if that method guarantees that the file will be
deleted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top