Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default Creating executable jar file in Eclipse

    Hi,
    I have just 3 files:

    TestJar.java - this has my main()

    TestJarForm - this has my form/AWT/Swing components - basically just a button that says press me, when pressed it plays bark.wav from the sounds file (next file)

    Sounds.java

    So I clicked on file->export->Java->Runnable Jar File->
    picked TestJar
    then gave the jarfile name as Test4
    and specified the directory where it should be created.

    I opened the jar file and I see a crapload of other files that are not related to these three. Basically, it jarred ALL the files in the project folder - how do I limit it to jar just these 3?

    Can I just delete these extra files from the jar?

    And finally double clicking the jar file... nothing happens :(
    The jar file seems to be associated with Nokia's OVI and that instead launches :(

    (I am on Windows 7)


    If need be I can copy and paste the contents of the 3 files here...

    Thanks!
    R

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What are you using to create your Jar? Eclipse? NetBeans? Other?
    In the subject I wrote:
    Creating executable jar file in Eclipse

    D:D
    But I guess I should have mentioned it in the body as well.

    Anyway, I think I have partly figured out why I was getting that problem, in Eclipse it seems to jar the entire project folder.
    So I created another project folder called "test" and moved the 3 files into that folder and was able to jar it like that... but still cant make an "executable jar file" :(

    A little off topic:
    I basically want to send a friend of mine the test project I was working on so that he (who does not have any java/programming experience) can double click my jar file and launch the program and test it out.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Why don't you go the traditional way. Create the jar file manually from command line, set the manifest file entries properly and run the jar.

    Refer following links in case if you are interested.

    How To Create Jar And Manifest Files Basics

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks for those links, I am totally new to this so they do help.

    I have also read online that you can create a zip file and then drag and drop the files accordingly, once done (and after adding the manifest file) you simple rename it to .jar

    have your tried this option and do you recommend it?

    Most importantly, if I distribute this jar file will my end users be able to just double click and launch it?

    Thanks!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    First on Eclipse. When you selected file->export->Java->Runnable Jar File->... the resulting dialog gave you a choice (on the left hand side) to select what resources you wanted to include.

    I wouldn't recommend using some random zip tool (including whatever Microsoft build into their file browser) when what you are making is a jar file. The links explain what the jar executable does and how to use it: and building your own jar file will give you more insight into what Eclipse does for you. (the options you have, which - as you've seen - may not be clear from its dialogs)

    For your friend to be able to doubleclick-n-run (that wasn't off topic btw) a couple of conditions have to be met. First you must have jarred up all the resources your application needs. And not just what you think it needs. You can test this by copying the jar file somewhere else on your computer, renaming it and seeing for yourself what happens.

    Secondly your friend's computer must have some sort of desktop manager that interprets jar files as something that should be run when double clicked. Windows (out of the box and provided the Java runtime is installed!) does, others may not.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by N00Bie View Post
    Most importantly, if I distribute this jar file will my end users be able to just double click and launch it?
    Yes, they will be able to do that. Read the conditions mentioned by pbrockway2 above.

    This link will probably clear all you doubts : How To Make An Executable Jar File.

    Google is probably the best thing, that has happened to us. Please utilize it properly. :D

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by N00Bie View Post
    Anyway, I think I have partly figured out why I was getting that problem, in Eclipse it seems to jar the entire project folder.
    So I created another project folder called "test" and moved the 3 files into that folder and was able to jar it like that... but still cant make an "executable jar file" :(
    If you right click on a project and select Export>Jar File, Eclipse assumes that you want to export all the files in that project; the wizard shows that project top level folder as selected in the left pane. Files unrelated, but belonging to that project are shown in the right pane. You can (de) select any directory in the project. You can also specify the name and directory of your jar file in the current panel (below the two others).

    In the next panels (click 'next') you can specify what exactly to export and you can also specify which file to use for the manifest entry in the jar file.

    Finally click 'Finish' and voila.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    First on Eclipse. When you selected file->export->Java->Runnable Jar File->... the resulting dialog gave you a choice (on the left hand side) to select what resources you wanted to include.
    I am not totally sure what you are talking about as I seem to be getting a different screen.. so pictures should help:







    Click the above images for full screen.

    Pic 1: For some reason I am getting some exclamation / warning icons on some of my files (see in the left panel) as well as red X's (errors?)

    Pic 2 and 3 are me making the Jar file...


    @goldest,
    Ok, will try that as well
    @Jos, my export seems a bit different from what you describe
    Last edited by N00Bie; 02-23-2011 at 02:45 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Select export to a jar file instead of a runnable jar file and do it yourself: i.e. select the files to be exported and an optional manifest file. More important: read the manual so you know what you're doing; too many people try to click their way through quite a complicated process (Eclipse already makes it a lot easier for you). Also read my previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Ok, some progress!

    I opened the jar file and I see a lot of class files for one single java file

    example
    one.java is the original file but I see
    one.class
    one0.class
    one1.class
    etc

    so I did a manual compile for each program
    (javac one.java)

    then jarred all those class files, made the manifest to point to the main() class

    (so far so good)

    then did a java -jar <filename>.jar (in the console)

    and the damn thing opened up and ran perfectly!!!

    Was soooo ****ing happy to see it running by itself
    (first time I have done this, and like they say... you never forget your first :p )

    Then I exited the console and double clicked it in windows explorer, and it opens Nokia OVI :confused: :eek: :(
    Started googling and found out that Nokia associates .jar files with OVI because they automatically assume that a .jar file is a game or something and NOT that it can be a java developer creating his own jars.
    ARRRGH! (Was not going to buy a Nokia again, and this seals the deal - next phone Android)

    Ok, done with my little rant... can someone tell me how I associate the .jar files back to java please?
    Is it as simple as "open with" and point to Java.exe or javaw.exe or do I have to add some parameters as well?

    Thanks!

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That's an OS dependent thing, and not a Java thing. Are you using Windows? If so, I believe you can change associations via a menu in the windows' file explorer.

  13. #13
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    That's an OS dependent thing, and not a Java thing. Are you using Windows? If so, I believe you can change associations via a menu in the windows' file explorer.
    Hey!
    Yes, win7.
    Yep, but change it to open with Java.exe or Javaw.exe and do I need to pass some parameters as well (for example -jar)

    Thanks!

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    <quote>I need to pass some parameters as well</quote>

    I was wondering about this...

    COntrol Panel -> Programs -> Default Programs -> Set Associations (or right click and "open with...") lets you set the application executable but that's all as far as I can see.

    According to annoyances.org the pre vista ability to edit these things has been removed. They suggest a third party tool that attempts the sisyphean task of making this OS behave properly and let you set file associations. I haven't tried it.

  15. #15
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    This is how I associated the jar file to java.exe

    Right click on jar file, in the menu that pops up click on open with, then browse (because java was not shown in the list of program) browse over to your installation of java and double click on java.exe

    problem is, when I double click on the .jar it opens a command window and then exits again (too fast to see what the error is), I think its a null pointer exception.

    But opening the same thing with java -jar <filename>.jar runs it perfectly :(

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  17. #17
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Nope, this is what I am getting:

    (click to expand)

    Tried running the exact same thing with
    java -jar test5.jar and it works perfectly.

    Manifest file is:

    Manifest-Version: 1.0
    Class-Path: .
    Main-Class: TestJar

  18. #18
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The manifest file has to end in a new line or carridge return. (ie have a blank line at the end).

  19. #19
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    The manifest file has to end in a new line or carridge return. (ie have a blank line at the end).
    Yep, every tut and result on google warns of this... and I checked 5 times, thats not the problem. :(

  20. #20
    N00Bie is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Ok, got it working!

    But had to download and use this:
    Jarfix

    It's a bit frustrating though, I prefer to know _exactly_ what this tool is doing rather than doing a double click and the program doing some "magic" to fix the paths.

    I'm guessing its not just a path issue (as I had changed the paths properly) but something in the registry as well...

    Anyway, if it helps others, just the link above.

Page 1 of 2 12 LastLast

Similar Threads

  1. create windows executable using executable jar file
    By sarwar1234 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 08:29 PM
  2. Eclipse and creating .jar file
    By wantakill in forum Eclipse
    Replies: 11
    Last Post: 09-04-2010, 11:51 PM
  3. creating war file in eclipse
    By pelegk1 in forum Eclipse
    Replies: 3
    Last Post: 05-24-2010, 04:28 AM
  4. Creating an executable jar file in IntelliJ IDEA
    By turanan in forum IntelliJ IDEA
    Replies: 0
    Last Post: 12-04-2009, 03:06 PM
  5. Need help for creating executable xml
    By patilsubh in forum XML
    Replies: 3
    Last Post: 10-01-2009, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •