Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

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

1008387May 21 2013 — edited May 26 2013
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2013
Added on May 21 2013
7 comments
4,504 views