7 Replies Latest reply: May 26, 2013 2:12 PM by 1008387 RSS

    How to create and use library JAR files with command-line tools?

    1008387
      Development Tools -> General Questions:

      I am trying to figure out how to put utility classes into JAR files and then compile and run applications against those JAR files using the command-line javac, jar, and java tools. I am using jdk1.7.0_17 on Debian GNU/Linux 6.0.7.

      I have posted a simple example with one utility class, one console application class, and a Makefile:

      http://holgerdanske.com/users/dpchrist/java/examples/jar-20130520-2134.tar.gz

      Here is a console session:
      2013-05-20 21:39:01 dpchrist@desktop ~/sandbox/java/jar
      $ cat src/com/example/util/Hello.java 
      package com.example.util;
      public class Hello {
          public static void hello(String arg) {
           System.out.println("hello, " + arg);
          }
      }
      
      2013-05-20 21:39:12 dpchrist@desktop ~/sandbox/java/jar
      $ cat src/com/example/hello/HelloConsole.java 
      package com.example.hello;
      import static com.example.util.Hello.hello;
      public class HelloConsole {
          public static void main(String [] args) {
           hello("world!");
          }
      }
      
      2013-05-20 21:39:21 dpchrist@desktop ~/sandbox/java/jar
      $ make
      rm -f hello
      find . -name '*.class' -delete
      javac src/com/example/util/Hello.java
      javac -cp src src/com/example/hello/HelloConsole.java
      echo "java -cp src com.example.hello.HelloConsole" > hello
      chmod +x hello
      
      2013-05-20 21:39:28 dpchrist@desktop ~/sandbox/java/jar
      $ ./hello
      hello, world!
      I believe I am looking for:

      1. Command-line invocation of "jar" to put the utility class bytecode file (Hello.class) into a JAR?

      2. Command-line invocation of "javac" to compile the application (HelloConsole.java) against the JAR file?

      3. Command-line invocation of "java" to run the application (HelloConsole.class) against the JAR file?

      I already know how t compile the utility class file.

      Any suggestions?

      TIA,

      David
        • 1. Re: How to create and use library JAR files with command-line tools?
          EJP
          So what part of the Tool documentation don't you understand?

          And don't use 'make', it's obsolete, and was never suitable for Java. Use Ant or Maven.
          • 2. Re: How to create and use library JAR files with command-line tools?
            gimbal2
            EJP wrote:
            And don't use 'make', it's obsolete, and was never suitable for Java. Use Ant or Maven.
            Re: packages and JAR files

            Deja vu.
            • 3. Re: How to create and use library JAR files with command-line tools?
              EJP
              Deja vu.
              All over again.
              • 4. Re: How to create and use library JAR files with command-line tools?
                1008387
                EJP wrote:
                So what part of the Tool documentation don't you understand?
                Primarily, the use of the JAR file:
                2. Command-line invocation of "javac" to compile the application (HelloConsole.java) against the JAR file?
                3. Command-line invocation of "java" to run the application (HelloConsole.class) against the JAR file?
                David
                • 5. Re: How to create and use library JAR files with command-line tools?
                  1008387
                  gimbal2 wrote:
                  Re: packages and JAR files
                  Deja vu.
                  The question(s) still haven't been answered correctly.

                  David
                  • 6. Re: How to create and use library JAR files with command-line tools?
                    1008387
                    I finally figured it out:

                    1. All name spaces must match -- identifiers, packages, file system, JAR contents, etc..

                    2. Tools must be invoked from specific working directories with specific option arguments, all according to the project name space.

                    My key discovery was that if the code says
                    import com.example.util.Hello;
                    then the JAR must contain
                    com/example/util/Hello.class
                    and I must invoke the compiler and interpreter with an -classpath argument that is the full path to the JAR file
                    -classpath ext/com/example/util.jar
                    The code is here:

                    http://holgerdanske.com/users/dpchrist/java/examples/jar-20130525-1301.tar.gz

                    Here is a console session that demonstrates building and running the code two ways:

                    1. Compiling the utility class into bytecode, compiling the application class against the utility bytecode, and running the application bytecode against the utility bytecode.

                    2. Putting the (previously compiled) utility bytecode into a JAR and running the application bytecode against the JAR. (Note that recompiling the application against the JAR was unnecessary.)

                    (If you don't know Make, understand that the working directory is reset to the initial working directory prior to each and every command issued by Make):
                    2013-05-25 14:02:47 dpchrist@desktop ~/sandbox/java/jar
                    $ cat apps/com/example/hello/Console.java 
                    package com.example.hello;
                    import com.example.util.Hello;
                    public class Console {
                        public static void main(String [] args) {
                         Hello.hello("world!");
                        }
                    }
                    
                    2013-05-25 14:02:55 dpchrist@desktop ~/sandbox/java/jar
                    $ cat libs/com/example/util/Hello.java 
                    package com.example.util;
                    public class Hello {
                        public static void hello(String arg) {
                         System.out.println("hello, " + arg);
                        }
                    }
                    
                    2013-05-25 14:03:03 dpchrist@desktop ~/sandbox/java/jar
                    $ make
                    rm -rf bin ext obj
                    mkdir obj
                    cd libs; javac -d ../obj com/example/util/Hello.java
                    mkdir bin
                    cd apps; javac -d ../bin -cp ../obj com/example/hello/Console.java
                    cd bin; java -cp .:../obj com.example.hello.Console
                    hello, world!
                    mkdir -p ext/com/example
                    cd obj; jar cvf ../ext/com/example/util.jar com/example/util/Hello.class
                    added manifest
                    adding: com/example/util/Hello.class(in = 566) (out= 357)(deflated 36%)
                    cd bin; java -cp .:../ext/com/example/util.jar com.example.hello.Console
                    hello, world!
                    
                    2013-05-25 14:03:11 dpchrist@desktop ~/sandbox/java/jar
                    $ tree -I CVS .
                    .
                    |-- Makefile
                    |-- apps
                    |   `-- com
                    |       `-- example
                    |           `-- hello
                    |               `-- Console.java
                    |-- bin
                    |   `-- com
                    |       `-- example
                    |           `-- hello
                    |               `-- Console.class
                    |-- ext
                    |   `-- com
                    |       `-- example
                    |           `-- util.jar
                    |-- libs
                    |   `-- com
                    |       `-- example
                    |           `-- util
                    |               `-- Hello.java
                    `-- obj
                        `-- com
                            `-- example
                                `-- util
                                    `-- Hello.class
                    
                    19 directories, 6 files
                    HTH,

                    David
                    • 7. Re: How to create and use library JAR files with command-line tools?
                      1008387
                      I figured out how to put the JAR file into the <jre>/lib/ext directory so that "javac" and "java" can find the utility bytecode without needing to specify the path to the JAR file as an -classpath argument. Here's a console session:
                      2013-05-26 12:04:23 dpchrist@desktop ~/sandbox/java/jar
                      $ make
                      rm -rf bin ext obj /home/dpchrist/jdk1.7.0_17/jre/lib/ext/com_example_util.jar
                      mkdir obj
                      cd libs; javac -d ../obj com/example/util/Hello.java
                      mkdir bin
                      cd apps; javac -d ../bin -cp ../obj com/example/hello/Console.java
                      cd bin; java -cp .:../obj com.example.hello.Console
                      hello, world!
                      mkdir ext
                      cd obj; jar cvf ../ext/com_example_util.jar com/example/util/Hello.class
                      added manifest
                      adding: com/example/util/Hello.class(in = 566) (out= 357)(deflated 36%)
                      rm -f bin/com/example/hello/Console.class
                      cd apps; javac -d ../bin -cp ../ext/com_example_util.jar com/example/hello/Console.java
                      cd bin; java -cp .:../ext/com_example_util.jar com.example.hello.Console
                      hello, world!
                      cp ext/com_example_util.jar /home/dpchrist/jdk1.7.0_17/jre/lib/ext/com_example_util.jar
                      cd apps; javac -d ../bin com/example/hello/Console.java
                      cd bin; java com.example.hello.Console
                      hello, world!
                      
                      2013-05-26 12:10:17 dpchrist@desktop ~/sandbox/java/jar
                      $ tree -I CVS .
                      .
                      |-- Makefile
                      |-- apps
                      |   `-- com
                      |       `-- example
                      |           `-- hello
                      |               `-- Console.java
                      |-- bin
                      |   `-- com
                      |       `-- example
                      |           `-- hello
                      |               `-- Console.class
                      |-- ext
                      |   `-- com_example_util.jar
                      |-- libs
                      |   `-- com
                      |       `-- example
                      |           `-- util
                      |               `-- Hello.java
                      `-- obj
                          `-- com
                              `-- example
                                  `-- util
                                      `-- Hello.class
                      
                      17 directories, 6 files
                      
                      2013-05-26 12:10:21 dpchrist@desktop ~/sandbox/java/jar
                      $ ll /home/dpchrist/jdk1.7.0_17/jre/lib/ext/com_example_util.jar 
                      -rw-r--r-- 1 dpchrist dpchrist 846 May 26 12:10 /home/dpchrist/jdk1.7.0_17/jre/lib/ext/com_example_util.jar
                      Here's the code:

                      http://holgerdanske.com/users/dpchrist/java/examples/jar-20130526-1205.tar.gz

                      HTH,

                      David