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 15 of 15

Thread: Creating a Jar executable java program

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Creating a Jar executable java program

    Hi,

    I created a simple quiz program which includes audio MP3 files and image JPG files. Most importantly I used an external package in order to play audio files (JavaFX package) to the quiz program. I've used JGrasp IDE to write the program in.

    I have tried to then create a jar file from the command line using the 'jar' command. Although before I used javaFX, the jar file could run perfectly on the system without the IDE. But now with the javaFX external package included, after created the jar file, and compiling the file, I get an exception saying cannot find the classes used in javaFX?

    I also tried to add the jar files that my IDE has worked with to compile the classes within javaFX, but still gives that exception when I try to compile the jar file (using command ' java -jar quiz.jar ') ?

    Can anyone tell how to add the javaFX external package to the jar file in order to run successfully on the system?

    Thanks, much appreciated


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    Are the javaFX classes in a jar file?
    Can you distribute your program as two jar files: yours and javaFX?
    The manifest file has an entry: Class-Path: that you can use to refer to the javaFX jar file.
    When using the java command with the -jar option to execute your program, the java command will be able to find the javaFX classes in the jar via the Class-Path: line.
    Here's the contents of a manifest file I use:
    Main-Class: CheckClassRefs
    Class-Path: DocumentViewerWParser.jar
    It requires the DocumentViewerWParser.jar file be in the same folder.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Thanks for the reply.

    Well there are exactly 4 jar files that I'm using for javaFX contained in the one jar file I'm trying to create.
    I'm not sure how to distribute them separately, so there are 4 jars (javaFX) contained into 1 jar (mine).
    In the manifest file I have used one of the main GUI classes as the 'Main-Class' as the entry point.
    I don't have any class path used in the file. Do I need to fill in that parameter to locate javaFX?
    What is the DocumentViewerWParser.jar file suppose to be?

    Thanks again

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    What is the DocumentViewerWParser.jar file suppose to be?
    That was from a manifest file I used for a program that used classes from the DocumentViewerWParser.jar file. It was an example of Class-Path: usage.

    I've seen a custom classloader that will load classes from a jar file that is contained in another jar file. Ask google.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Thanks for the reply. I actually managed to get the jar executing successfully now by adding each jar file to the class-path in the manifest.
    Although now when I copy that jar file to another directory and try execute it, it doesn't run? The jar file somehow needs the originating files (.class's, image's, audio's) to execute successfully. I thought the whole point of creating jar files is to be able to execute them independently without it's originating resource files?

    Thanks again

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    without it's originating resource files?
    That sounds like magic. The point of a jar file is to contain ALL the resources needed by the program.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Yes, the jar file does contain all it's resources. As it works ok and executes. But when I move the jar file elsewhere from the directory where it was created, it wont execute. That's my new problem

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    , it wont execute
    What errors do you get?
    Open a command prompt window, change to the folder with the jar file and enter:
    java -jar THEJARFILENAME.jar
    copy the contents of the window and paste it here.

    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Thanks for the reply again.

    Because the jar files executes in the same directory where the file was created, I copied the output of the successful result below

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\dougie1809\Desktop\ProgrammingExamples\Qu iz>java -jar FunQuiz.jar

    C:\Users\dougie1809\Desktop\ProgrammingExamples\Qu iz>

    Now I moved the jar file back 1 directory and now I got the output with an exception below:

    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation. All rights reserved.

    C:\Users\dougie1809\Desktop\ProgrammingExamples\Qu iz>java -jar FunQuiz.jar

    C:\Users\dougie1809\Desktop\ProgrammingExamples\Qu iz>cd ..

    C:\Users\dougie1809\Desktop\ProgrammingExamples>ja va -jar FunQuiz.jar
    Exception in thread "main" MediaException: MEDIA_UNAVAILABLE : C:\Users\dougie18
    09\Desktop\ProgrammingExamples\menu.mp3 (The system cannot find the file specifi
    ed)
    at javafx.scene.media.Media.<init>(Unknown Source)
    at QuizMenu.<init>(QuizMenu.java:119)
    at QuizMenu.main(QuizMenu.java:216)

    C:\Users\dougie1809\Desktop\ProgrammingExamples>

    Thanks again

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    C:\Users\dougie1809\Desktop\ProgrammingExamples\me nu.mp3 (The system cannot find the file specifi
    ed)
    at javafx.scene.media.Media.<init>(Unknown Source)
    at QuizMenu.<init>(QuizMenu.java:119)
    Yes, the jar file does contain all it's resources
    Is that file one of the resources that is in the jar file? How is the code trying to read it? It needs to use a URL not a File object to access it.
    What code is at line 119?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Yes that file is in the jar file. I'm not sure how it is reading these files, I just don't understand why the file won't execute when removed from the directory where the jar file was created. For some reason, the jar file needs all the resource files (which are already contained inside the jar file) beside it in the same directory to execute.

    The code below is line 119 which takes in the String 'song' (name of file) to construct Media.
    audio = new Media(song);

    But all of this code does work, until I move the jar file elsewhere from the directory it was originally created in. (Also that song file is contained inside the jar file)

    Thanks again

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    What is the definition for the Media constructor? Will it take a URL?
    What is the contents of the song variable? Is it a URL pointing to the location of a file in the jar file?

    If the Media constructor requires a path to a file, then it will not look in the jar file.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Yes it can take in a URL, FILE or URI as a String. The code below is the full value of the String:

    String song = new File("menu.mp3").toURI().toString();

    This interpretation of the String song eliminates the need to use full path names and can determine the name of the file as long as it is contained in the current directory, which in this case is.

    Thanks again

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating a Jar executable java program

    You can't use the File class when using jar files. The File class looks on the disk, not in the jar file.

    Look at the Class class's getResource() method for getting the URL of a file that is in the jar file. It looks on the classpath which is set to the jar file.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    57
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Jar executable java program

    Oh right, thanks. I'l look into that. I never knew that how your code could be the problem with creating jar files.

    Thanks again

Similar Threads

  1. No sound in executable jar
    By BestSanchez in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 25th, 2012, 06:01 PM
  2. Making Executable Jar Files.
    By Java Programmer in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: January 19th, 2012, 12:14 AM
  3. .jar executable
    By mwr76 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 9th, 2011, 11:27 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