Results 1 to 15 of 15
  1. #1
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default Creating a Jar File Using Command Prompt

    Q1) Dear All, I have read the Java API and my classes are stored in a folder named CPE102. However, Windows XP refuse to acknowledge my command. Tell me what is wrong?
    Refer to help.jpg

    Q2) I have downloaded the JavaRunner file from the Java website. However, I am not sure where to pass the arguments. How am I siuppose to tell the program which file I wanna read?

    In other words, where to pass the URL if I am accessing a local directory?

    import java.io.IOException;
    import java.net.URL;
    import java.net.MalformedURLException;
    import java.lang.reflect.InvocationTargetException;

    /**
    * Runs a jar application from any url. Usage is 'java JarRunner url [args..]'
    * where url is the url of the jar file and args is optional arguments to
    * be passed to the application's main method.
    */
    public class JarRunner {
    public static void main(String[] args) {
    if (args.length < 1) {
    usage();
    }
    URL url = null;
    try {
    url = new URL(args[0]);
    } catch (MalformedURLException e) {
    fatal("Invalid URL: " + args[0]);
    }
    // Create the class loader for the application jar file
    JarClassLoader cl = new JarClassLoader(url);
    // Get the application's main class name
    String name = null;
    try {
    name = cl.getMainClassName();
    } catch (IOException e) {
    System.err.println("I/O error while loading JAR file:");
    e.printStackTrace();
    System.exit(1);
    }
    if (name == null) {
    fatal("Specified jar file does not contain a 'Main-Class'" +
    " manifest attribute");
    }
    // Get arguments for the application
    String[] newArgs = new String[args.length - 1];
    System.arraycopy(args, 1, newArgs, 0, newArgs.length);
    // Invoke application's main class
    try {
    cl.invokeClass(name, newArgs);
    } catch (ClassNotFoundException e) {
    fatal("Class not found: " + name);
    } catch (NoSuchMethodException e) {
    fatal("Class does not define a 'main' method: " + name);
    } catch (InvocationTargetException e) {
    e.getTargetException().printStackTrace();
    System.exit(1);
    }
    }

    private static void fatal(String s) {
    System.err.println(s);
    System.exit(1);
    }

    private static void usage() {
    fatal("Usage: java JarRunner url [args..]");
    }
    }

    In short, I just want to create a jar file and then run it automatically using a click. HELP
    Attached Thumbnails Attached Thumbnails Creating a Jar File Using Command Prompt-help.jpg  
    Last edited by hitmen; 03-08-2009 at 12:55 PM.

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The problem is that windows doesn't know where the 'jar' executable is.

    Go to your windows control panel, open up "system", click the advanced tab, and then the 'environment variables' button. Add the path to your jdk's bin directory to the 'PATH' variable, and then the command prompt will find the 'jar' command.

    I don't know why the java installer doesn't set this up automatically, but it's very annoying.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by toadaly View Post
    I don't know why the java installer doesn't set this up automatically, but it's very annoying.
    I think this is happen on SDK installation, not with the JDK installation. JDK comes with jar bundle too.

  4. #4
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    It still doesnt work.Look at my screenshot below
    BTW what is the diff btw a classpath and a path
    and what is the difference btw a JDK and SDK? I thought they are the same

    And is there a way to create a JAR file without using command prompt?
    Attached Thumbnails Attached Thumbnails Creating a Jar File Using Command Prompt-help.jpg  

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by hitmen View Post
    It still doesnt work.
    On what contest you are talking about? Did you set the path as toadaly explain. Can you show the path you have added there?

    Quote Originally Posted by hitmen View Post
    what is the difference btw a JDK and SDK? I thought they are the same
    Both are development kits,
    JDK - Java Development Kit
    SDK - Software Development Kit

    But JDK contain JRE which can/allow distributing with your Java application.


    Quote Originally Posted by hitmen View Post
    BTW what is the diff btw a classpath and a path
    classpath define in the Path field of the system variables. Other than that there is no major difference, only for the simplicity use them.

    Quote Originally Posted by hitmen View Post
    And is there a way to create a JAR file without using command prompt?
    It's possible with use of an IDE. Almost all Java IDEs can build the executable jar files.

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by hitmen View Post
    It still doesnt work.Look at my screenshot below
    You're screenshot does not show that the jdk is in your PATH. If you cut and paste the entire thing, we can help you.

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

    Default

    Quote Originally Posted by hitmen View Post
    BTW what is the diff btw a classpath and a path
    Quote Originally Posted by Eranga View Post
    classpath define in the Path field of the system variables. Other than that there is no major difference, only for the simplicity use them.
    The terms path and classpath both refer to a collection of locations, or to a variable holding the textual representation of such a collection. (For instance in Windows the names of directories separated by the ; symbol).

    But they refer to very different collections with nothing in common.

    The path is the list of places that your OS will use when you specify the name of an executable. In your case if you use the command "jar" the OS will look for various files - jar.exe, jar.bat etc - in all the directories of your path.

    The classpath is the list of places the Java runtime will use when you specify a class name (or other resource). When you use a class name like mypackage.MyPanel then the runtime will look for the MyPanel class by searching the classpath for the mypackage package and, within it, the MyPanel class. The classpath can contain jar archives as well as directories. Yet another difference between the two is that a classpath variable is a waste of time as there are other, better, ways of specifying the classpath when the application is run.

    (As mentioned, your screenshot does not show what the PATH currently is. But the problem is that the jar.exe executable is not to be found within the directories listed in the PATH variable. Check spelling and that you've quoted any string with spaces in it.)
    Last edited by pbrockway2; 03-10-2009 at 07:48 AM.

  8. #8
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Cool

    Open the command prompt and change to the directory that your java files are in. Then, type "path" followed by a space and then the path to the bin folder of your jdk e.g. path C:\Program Files\Java\jdk1.6.0_05\bin

    Now, type the following:

    jar cf MyJar.jar *.java Folder1 Folder2

    javac *.java

    jar cmf mymanifest.txt MyJar.jar *.class

    --------------------------------------

    Note: If you want to include stuff(pics,etc) from subfolders of the current folder, just give the name as shown in the first instruction above. If you don't want to include stuff, leave out Folder 1 etc.

    The second instruction simply compiles the java files.

    As for the last instruction, make sure that you have a text file called mymanifest.txt, and in the text file, have the instruction Main-Class: folowed by the name of the class that has the main method(public static void main())

    e.g. Main-Class: MyProg

    Leave a blank line at the end of the text file, then save it

    You don't need to include .java

  9. #9
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    Darn. Previously, I pointed to a bin in netbeans that does not contain jar.exe. Now I found it in a "Java" folder. Wasted 2 days.

    Now this is the trouble I am facing.

    Look at the screenshot and see what is happening
    Attached Thumbnails Attached Thumbnails Creating a Jar File Using Command Prompt-help.jpg  
    Last edited by hitmen; 03-10-2009 at 12:50 PM.

  10. #10
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    Now there is another error message.
    I ensured that there is a Main-Class: MainApp

    with 2 lines before I closed the text file
    Note that MainApp is the name of my main class
    what is wrong?
    Attached Thumbnails Attached Thumbnails Creating a Jar File Using Command Prompt-help.jpg  

  11. #11
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Doctor Cactus View Post
    Open the command prompt and change to the directory that your java files are in. Then, type "path" followed by a space and then the path to the bin folder of your jdk e.g. path C:\Program Files\Java\jdk1.6.0_05\bin

    Now, type the following:

    jar cf MyJar.jar *.java Folder1 Folder2

    javac *.java

    jar cmf mymanifest.txt MyJar.jar *.class

    --------------------------------------

    Note: If you want to include stuff(pics,etc) from subfolders of the current folder, just give the name as shown in the first instruction above. If you don't want to include stuff, leave out Folder 1 etc.

    The second instruction simply compiles the java files.

    As for the last instruction, make sure that you have a text file called mymanifest.txt, and in the text file, have the instruction Main-Class: folowed by the name of the class that has the main method(public static void main())

    e.g. Main-Class: MyProg

    Leave a blank line at the end of the text file, then save it

    You don't need to include .java
    Dear Doctor Catcus,

    Did you have another folder that "wraps" your package?
    Is it necessary to place the jar file in the same folder as the IDE?
    (Currently,my java files are dragged to the desktop. I dunno if this is right or wrong)

    Thanks.

  12. #12
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default

    Tell me what I must add in my manifest

    Manifest-Version: 1.0
    Created-By: 1.5.0_07 (Sun Microsystems Inc.)
    Main-Class: CiphersApp

    Is there a path I am suppose to add?

  13. #13
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Generally, you will want a "Class-Path: " option in your manifest to specify other jar files your executable jar depends on, such as :

    Java Code:
    Class-Path: lib/a.jar lib/b.jar
    ...and you might want a splash screen too, something like...

    Java Code:
    SplashScreen-Image: images/splash.jpg

  14. #14
    killer8889 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    hey i found out a link to forum
    may be this could help you
    How to create JAR file
    Last edited by DarrylBurke; 08-27-2011 at 04:41 PM. Reason: Spam link removed

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

    Default

    may be this could help you
    Help who? This thread is ancient.

Similar Threads

  1. Running applet from command prompt
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:10 AM
  2. Replies: 2
    Last Post: 12-03-2008, 01:24 PM
  3. help me!!!! about command prompt..
    By kureikougaiji in forum New To Java
    Replies: 2
    Last Post: 11-13-2008, 06:15 PM
  4. Regular expressions from command prompt
    By GenkiSudo in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 02:40 PM
  5. Problem during executing Command Prompt
    By keshari in forum Advanced Java
    Replies: 4
    Last Post: 06-05-2008, 04:06 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
  •