Create an executable (.jar or .exe) using code
Hi..
Is it possible to create an executable file (either .jar or .exe) from Java code?
I did try to find it on google, but most of them were not using code..
For example, using export feature (in Eclipse) to create .jar and use another software to create .exe (e.g. Launch4j)..
Or using command prompt, etc.. These are not what i wanted..
So i wonder if there is a way to create this executable file through coding?
For example when i click a button, a .jar file will be created..
Could anyone give me a reference link to look at?
Please let me know if my question was not clear enough..
Thank you.. :)
Re: Create an executable (.jar or .exe) using code
Do you mean create a jar programmatically? You could call the jar command through Runtime.exec(), although not all users may have this installed. Alternatively, take a look at JarOutputStream (Java Platform SE 6) . See java - How to use JarOutputStream to create a JAR file? - Stack Overflow for code examples
Re: Create an executable (.jar or .exe) using code
Quote:
Originally Posted by
copeg
yes, what i meant was how to create an executable .jar file programmatically.. sorry for my poor english.. :)
i have gone through the link you mentioned, and it works ! although i still got some errors (corrupted .jar file), but i will try to understand it..
thank you so much ! :)
Re: Create an executable (.jar or .exe) using code
it's me again..
so the problem now is the source file that i want to insert to the .jar file..
is there a way to insert a particular file without inserting the whole path?
for example i want to insert a .class file in bin directory, so i put "bin/sample.class" as source..
but when i open the .jar file, it give me a folder name bin and sample.class in it..
i just want to take the sample.class..
i have tried to use .getName() but didn't work..
target.putNextEntry(new JarEntry(entry.getName()));
it still gives me a folder name bin with sample.class in it, just like before..
any idea?
and 1 more thing i want to ask..
is it necessary for the folder META-INF and the .class file to be in the same directory in order for .jar file to be executable?
because previously the .jar file that i created using export feature in Eclipse, the folder META-INF and .class file are in the same directory..
while in this case, the META-INF and .class file are not in the same directory because of the bin folder..
Re: Create an executable (.jar or .exe) using code
That's...odd... Try printing out entry.getName() to the console before you put it in the jar. I want to know if it is returning "sample.class" or if it actually is returning "bin/sample.class". If the former, then it means something else is happening. If the later, well then I don't know why it is doing that.
To answer your second question, I think it is ok, but you probably need to change the Main-Class attribute to reflect the folder. But I'm, not 100% about this one either. It seems more like a try-and-see situation.
Re: Create an executable (.jar or .exe) using code
Quote:
Originally Posted by
aussiemcgr
That's...odd... Try printing out entry.getName() to the console before you put it in the jar. I want to know if it is returning "sample.class" or if it actually is returning "bin/sample.class". If the former, then it means something else is happening. If the later, well then I don't know why it is doing that.
To answer your second question, I think it is ok, but you probably need to change the Main-Class attribute to reflect the folder. But I'm, not 100% about this one either. It seems more like a try-and-see situation.
hi.. thanks for the reply..
i have solved the problem..
so previously what i did was
Code java:
JarEntry entry = new JarEntry(source);
entry.setTime(source.lastModified());
target.putNextEntry(entry.getName());
target.closeEntry();
this couldn't work..
but after i change it to this, it works..
Code java:
JarEntry entry = new JarEntry(source.getName());
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
thanks for the help.. :)
Re: Create an executable (.jar or .exe) using code
-sorry for doubleposting-