Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Create an executable (.jar or .exe) using code

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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..


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. The Following User Says Thank You to copeg For This Useful Post:

    nosxlimit (May 29th, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Create an executable (.jar or .exe) using code

    Quote Originally Posted by copeg View Post
    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
    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 !
    Last edited by nosxlimit; May 29th, 2012 at 10:56 PM.

  5. #4
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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..

  6. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    nosxlimit (May 31st, 2012)

  8. #6
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Create an executable (.jar or .exe) using code

    Quote Originally Posted by aussiemcgr View Post
    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
    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..
    JarEntry entry = new JarEntry(source.getName());
    					entry.setTime(source.lastModified());
    					target.putNextEntry(entry);
    					target.closeEntry();

    thanks for the help..

  9. #7
    Junior Member
    Join Date
    May 2012
    Location
    Singapore
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Create an executable (.jar or .exe) using code

    -sorry for doubleposting-

Similar Threads

  1. Making Executable Jar Files.
    By Java Programmer in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: January 19th, 2012, 12:14 AM
  2. .jar executable
    By mwr76 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 9th, 2011, 11:27 AM
  3. BufferedImage create flicker in simple code
    By arnbook in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 12:22 AM
  4. Jar Executable File
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 43
    Last Post: June 23rd, 2010, 03:53 PM
  5. Making executable JAR more "executable"
    By ni4ni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 01:19 PM