Forum Stats

  • 3,734,717 Users
  • 2,247,030 Discussions
  • 7,857,467 Comments

Discussions

packages and JAR files

1008387
1008387 Member Posts: 20
New to Java:

I am trying to learn about packages and JAR libraries using jdk1.7.0_17 on Debian 6.0.7 (Squeeze). I have created a tree of directories and files to work with:
2013-05-11 20:22:07 [email protected] ~/sandbox/java/jar
$ tree .
.
|-- Makefile
|-- bin
|   |-- using_class_files
|   |   `-- MyApp.class
|   `-- using_jar_file
|-- ext
|   `-- com
|       `-- example
|           `-- myapp
|               `-- util.jar
|-- obj
|   `-- com
|       `-- example
|           `-- myapp
|               `-- util
|                   `-- Speak.class
`-- src
    `-- com
        `-- example
            `-- myapp
                |-- MyApp.java
                `-- util
                    `-- Speak.java

17 directories, 6 files

2013-05-11 20:22:39 [email protected] ~/sandbox/java/jar
$ cat src/com/example/myapp/util/Speak.java 
package com.example.myapp.util;
public class Speak {
    public static void hello(String arg) {
	System.out.println("hello, " + arg);
    }
}

2013-05-11 20:23:27 [email protected] ~/sandbox/java/jar
$ cat src/com/example/myapp/MyApp.java 
import com.example.myapp.util.Speak;
public class MyApp {
    public static void main(String [] args) {
	Speak s = new Speak();
	s.hello("world!");
    }
}

2013-05-11 20:23:54 [email protected] ~/sandbox/java/jar
$ cat Makefile 
.PHONY: all
all: clean using_class_files using_jar_file

.PHONY: using_class_files
using_class_files:
	# create utility bytecode file (Speak.class)
	mkdir -p obj/com/example/myapp/util
	javac -d obj src/com/example/myapp/util/Speak.java

	# compile application file against Speak.class
	mkdir -p bin/using_class_files
	javac -d bin/using_class_files -cp obj src/com/example/myapp/MyApp.java

	# run application
	java -cp obj:bin/using_class_files MyApp

.PHONY: using_jar_file
using_jar_file:
	# put Speak.class into a Java archive (util.jar)
	mkdir -p ext/com/example/myapp
	jar cvf ext/com/example/myapp/util.jar -C obj/com/example/myapp/util Speak.class

	# print util.jar table of contents
	jar tf ext/com/example/myapp/util.jar

	# compile application against util.jar
	mkdir -p bin/using_jar_file
	javac -d bin/using_jar_file -extdirs ext src/com/example/myapp/MyApp.java

.PHONY: clean
clean:
	rm -rf bin ext obj
I am using Make so that my commands are repeatable; the Makefile is "quick and dirty". The focus is on the Java "package" and "import" keywords and the command line tools and their options (javac, jar, and java).

Compiling the utility class (Speak.java), compiling the application (MyApp.java) against the bytecode tree (./obj), and running the application against the bytecode tree work:
2013-05-11 20:23:58 [email protected] ~/sandbox/java/jar
$ make clean
rm -rf bin ext obj

2013-05-11 20:24:01 [email protected] ~/sandbox/java/jar
$ make using_class_files
# create utility bytecode file (Speak.class)
mkdir -p obj/com/example/myapp/util
javac -d obj src/com/example/myapp/util/Speak.java
# compile application file against Speak.class
mkdir -p bin/using_class_files
javac -d bin/using_class_files -cp obj src/com/example/myapp/MyApp.java
# run application
java -cp obj:bin/using_class_files MyApp
hello, world!
Putting the utility bytecode file (Speak.class) into a JAR file (util.jar) works, but compiling the application against the JAR tree (./ext) fails:
2013-05-11 20:24:08 [email protected]top ~/sandbox/java/jar
$ make using_jar_file
# put Speak.class into a Java archive (util.jar)
mkdir -p ext/com/example/myapp
jar cvf ext/com/example/myapp/util.jar -C obj/com/example/myapp/util Speak.class
added manifest
adding: Speak.class(in = 572) (out= 364)(deflated 36%)
# print util.jar table of contents
jar tf ext/com/example/myapp/util.jar
META-INF/
META-INF/MANIFEST.MF
Speak.class
# compile application against util.jar
mkdir -p bin/using_jar_file
javac -d bin/using_jar_file -extdirs ext src/com/example/myapp/MyApp.java
src/com/example/myapp/MyApp.java:1: error: package com.example.myapp.util does not exist
import com.example.myapp.util.Speak;
                             ^
src/com/example/myapp/MyApp.java:4: error: cannot find symbol
	Speak s = new Speak();
	^
  symbol:   class Speak
  location: class MyApp
src/com/example/myapp/MyApp.java:4: error: cannot find symbol
	Speak s = new Speak();
	              ^
  symbol:   class Speak
  location: class MyApp
3 errors
make: *** [using_jar_file] Error 1
Any suggestions?

TIA,

David

Best Answer

  • EJP
    EJP Member Posts: 32,919
    edited May 13, 2013 11:30PM Accepted Answer
    Normally you would run javac from your 'src' directory, so:
    javac [options] com/mycompany/whatever/package/MyClass.java
    Then it will automatically find and/or compile any other classes that are correctly located under 'src' according to their packages.

Answers

  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    In stead of using Make I would use ANT which is basically a cross-platform Java version of Make, optimized for a Java programming environment. Then you also have plenty of documentation available on how to properly set things up.
  • 1008387
    1008387 Member Posts: 20
    edited May 15, 2013 8:38PM
    I want to learn the command-line tools javac, jar, and java.

    Does anyone see the error in the example I posted?

    Is there a bug in one of the jdk1.7.0_17 tools?

    TIA,

    David

    Edited by: David Christensen on May 15, 2013 5:38 PM -- removed link to obsolete tarball
  • EJP
    EJP Member Posts: 32,919
    edited May 13, 2013 11:30PM Accepted Answer
    Normally you would run javac from your 'src' directory, so:
    javac [options] com/mycompany/whatever/package/MyClass.java
    Then it will automatically find and/or compile any other classes that are correctly located under 'src' according to their packages.
  • 1008387
    1008387 Member Posts: 20
    cd'ing into the ./src directory doesn't seem to help...
    2013-05-13 19:47:10 [email protected] ~/sandbox/java/jar
    $ cat Makefile
    build_dir=$(shell pwd)
    
    .PHONY: all
    all: clean using_class_files using_jar_file
    
    .PHONY: using_class_files
    using_class_files:
    	# create utility bytecode file (Speak.class)
    	mkdir -p obj/com/example/myapp/util
    	cd src; javac -d $(build_dir)/obj com/example/myapp/util/Speak.java
    
    	# compile application file against Speak.class
    	mkdir -p bin/using_class_files
    	cd src/com/example/myapp; javac -d $(build_dir)/bin/using_class_files -cp $(build_dir)/obj MyApp.java
    
    	# run application
    	java -cp obj:bin/using_class_files MyApp
    
    .PHONY: using_jar_file
    using_jar_file:
    	# put Speak.class into a Java archive (util.jar)
    	mkdir -p ext/com/example/myapp
    	jar cvf ext/com/example/myapp/util.jar -C obj/com/example/myapp/util Speak.class
    
    	# print util.jar table of contents
    	jar tf ext/com/example/myapp/util.jar
    
    	# compile application against util.jar
    	mkdir -p bin/using_jar_file
    	cd src/com/example/myapp; javac -d $(build_dir)/bin/using_jar_file -extdirs $(build_dir)/ext MyApp.java
    
    .PHONY: clean
    clean:
    	rm -rf bin ext obj
    	
    
    2013-05-13 19:47:12 [email protected] ~/sandbox/java/jar
    $ make all
    rm -rf bin ext obj
    # create utility bytecode file (Speak.class)
    mkdir -p obj/com/example/myapp/util
    cd src; javac -d /home/dpchrist/sandbox/java/jar/obj com/example/myapp/util/Speak.java
    # compile application file against Speak.class
    mkdir -p bin/using_class_files
    cd src/com/example/myapp; javac -d /home/dpchrist/sandbox/java/jar/bin/using_class_files -cp /home/dpchrist/sandbox/java/jar/obj MyApp.java
    # run application
    java -cp obj:bin/using_class_files MyApp
    hello, world!
    # put Speak.class into a Java archive (util.jar)
    mkdir -p ext/com/example/myapp
    jar cvf ext/com/example/myapp/util.jar -C obj/com/example/myapp/util Speak.class
    added manifest
    adding: Speak.class(in = 572) (out= 364)(deflated 36%)
    # print util.jar table of contents
    jar tf ext/com/example/myapp/util.jar
    META-INF/
    META-INF/MANIFEST.MF
    Speak.class
    # compile application against util.jar
    mkdir -p bin/using_jar_file
    cd src/com/example/myapp; javac -d /home/dpchrist/sandbox/java/jar/bin/using_jar_file -extdirs /home/dpchrist/sandbox/java/jar/ext MyApp.java
    MyApp.java:1: error: package com.example.myapp.util does not exist
    import com.example.myapp.util.Speak;
                                 ^
    MyApp.java:4: error: cannot find symbol
    	Speak s = new Speak();
    	^
      symbol:   class Speak
      location: class MyApp
    MyApp.java:4: error: cannot find symbol
    	Speak s = new Speak();
    	              ^
      symbol:   class Speak
      location: class MyApp
    3 errors
    make: *** [using_jar_file] Error 1
    
    2013-05-13 19:47:16 [email protected] ~/sandbox/java/jar
    $ tree -I CVS .
    .
    |-- Makefile
    |-- bin
    |   |-- using_class_files
    |   |   `-- MyApp.class
    |   `-- using_jar_file
    |-- ext
    |   `-- com
    |       `-- example
    |           `-- myapp
    |               `-- util.jar
    |-- obj
    |   `-- com
    |       `-- example
    |           `-- myapp
    |               `-- util
    |                   `-- Speak.class
    `-- src
        `-- com
            `-- example
                `-- myapp
                    |-- MyApp.java
                    `-- util
                        `-- Speak.java
    
    17 directories, 6 files
    It seems as if the -extdirs option to javac isn't working (?).

    Can anybody reproduce the results I obtained?
  • EJP
    EJP Member Posts: 32,919
    edited May 13, 2013 11:16PM
    cd'ing into the ./src directory doesn't seem to help...
    You're not doing that.
    cd src/com/example/myapp; javac -d $(build_dir)/bin/using_jar_file -extdirs $(build_dir)/ext MyApp.java
    You're doing something else of your own invention.

    Try it my way.
  • 1008387
    1008387 Member Posts: 20
    Please post your way -- e.g. perform your command in a console and cut and paste the command and the output into your browser in a reply to this post.
  • EJP
    EJP Member Posts: 32,919
    I wrote:
    Normally you would run javac from your 'src' directory
    Note that I said "'src' directory", not "source directory".
    You coded:
    cd src/com/example/myapp
    I wrote:
    javac [options] com/mycompany/whatever/package/MyClass.java
    You coded:
    javac [options] MyApp.java
    Can you see the difference now?
  • 1008387
    1008387 Member Posts: 20
    edited May 15, 2013 8:38PM
    I wrote an MS-DOS batch file makewin.bat:
    REM create utility bytecode file (Speak.class)
    REM mkdir obj\com\example\myapp\util
    javac -d obj src\com\example\myapp\util\Speak.java
    
    REM compile application file against Speak.class
    REM mkdir bin\using_class_files
    javac -d bin\using_class_files -cp obj src\com\example\myapp\MyApp.java
    
    REM run application
    java -cp "obj;bin\using_class_files" MyApp
    
    REM put Speak.class into a Java archive (util.jar)
    REM mkdir  ext\com\example\myapp
    jar cvf ext\com\example\myapp\util.jar -C obj\com\example\myapp\util Speak.class
    
    REM print util.jar table of contents
    jar tf ext\com\example\myapp\util.jar
    
    REM compile application against util.jar
    REM bin\using_jar_file
    javac -d bin\using_jar_file -extdirs ext src\com\example\myapp\MyApp.java
    I tested it on Windows XP SP3 with jdk-7u17-windows-i586 and obtained the same results:
    C:\cygwin\home\dpchrist\sandbox\java\jar>makewin.bat
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM create utility bytecode file (Speak
    .class)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM mkdir obj\com\example\myapp\util
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d obj src\com\example\myapp\util
    \Speak.java
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM compile application file against Sp
    eak.class
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM mkdir bin\using_class_files
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_class_files -cp obj
    src\com\example\myapp\MyApp.java
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM run application
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>java -cp "obj;bin\using_class_files" My
    App
    hello, world!
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM put Speak.class into a Java archive
     (util.jar)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM mkdir  ext\com\example\myapp
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>jar cvf ext\com\example\myapp\util.jar
    -C obj\com\example\myapp\util Speak.class
    added manifest
    adding: Speak.class(in = 572) (out= 364)(deflated 36%)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM print util.jar table of contents
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>jar tf ext\com\example\myapp\util.jar
    META-INF/
    META-INF/MANIFEST.MF
    Speak.class
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM compile application against util.ja
    r
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM bin\using_jar_file
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_jar_file -extdirs ex
    t src\com\example\myapp\MyApp.java
    src\com\example\myapp\MyApp.java:1: error: package com.example.myapp.util does n
    ot exist
    import com.example.myapp.util.Speak;
                                 ^
    src\com\example\myapp\MyApp.java:4: error: cannot find symbol
            Speak s = new Speak();
            ^
      symbol:   class Speak
      location: class MyApp
    src\com\example\myapp\MyApp.java:4: error: cannot find symbol
            Speak s = new Speak();
                          ^
      symbol:   class Speak
      location: class MyApp
    3 errors
    David

    Edited by: David Christensen on May 15, 2013 5:37 PM -- removed link to obsolete tarball
  • 1008387
    1008387 Member Posts: 20
    EJP wrote:
    Can you see the difference now?
    Yes, I saw the difference. Please see my post of May 13, 2013 7:53 PM. Note the command:
    cd src; javac -d /home/dpchrist/sandbox/java/jar/obj com/example/myapp/util/Speak.java
    That corresponds to your:
    javac [options] com/mycompany/whatever/package/MyClass.java
    David
  • EJP
    EJP Member Posts: 32,919
    Well if you stop trying all the ones that don't work and tell us what happened when you did that one, we might get somewhere. At the moment it's just a brain salad smorgasbord.
  • 1008387
    1008387 Member Posts: 20
    edited May 15, 2013 8:47PM
    EJP wrote:
    >
    ... tell us what happened when you did that one,
    >

    Which one? Please post your console session.

    I have fixed the makewin.bat file so that it correctly removes and then generates the bin, ext, and obj directories. The latest tarball is here:

    http://holgerdanske.com/users/dpchrist/java/examples/jar-20130515-1734.tar.gz

    Here is the console session when I run the batch file:
    C:\cygwin\home\dpchrist\sandbox\java\jar>makewin.bat
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM remove build directories
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>rmdir /s /q bin ext obj
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM create utility bytecode file (Speak
    .class)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir obj\com\example\myapp\util
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d obj src\com\example\myapp\util
    \Speak.java
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM compile application file against Sp
    eak.class
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir bin\using_class_files
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_class_files -cp obj
    src\com\example\myapp\MyApp.java
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM run application
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>java -cp "obj;bin\using_class_files" My
    App
    hello, world!
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM put Speak.class into a Java archive
     (util.jar)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir ext\com\example\myapp
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>jar cvf ext\com\example\myapp\util.jar
    -C obj\com\example\myapp\util Speak.class
    added manifest
    adding: Speak.class(in = 572) (out= 364)(deflated 36%)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM print util.jar table of contents
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>jar tf ext\com\example\myapp\util.jar
    META-INF/
    META-INF/MANIFEST.MF
    Speak.class
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM compile application against util.ja
    r
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir bin\using_jar_file
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_jar_file -extdirs ex
    t src\com\example\myapp\MyApp.java
    src\com\example\myapp\MyApp.java:1: error: package com.example.myapp.util does n
    ot exist
    import com.example.myapp.util.Speak;
                                 ^
    src\com\example\myapp\MyApp.java:4: error: cannot find symbol
            Speak s = new Speak();
            ^
      symbol:   class Speak
      location: class MyApp
    src\com\example\myapp\MyApp.java:4: error: cannot find symbol
            Speak s = new Speak();
                          ^
      symbol:   class Speak
      location: class MyApp
    3 errors
    David

    Edited by: David Christensen on May 15, 2013 5:47 PM -- change console session line endings to LF
  • EJP
    EJP Member Posts: 32,919
    edited May 15, 2013 9:46PM
    Which one?
    The one I told you to do. The only one. It's not as though I've given you 17 different things to do and you don't know which one. 'cd src; javac [options] com\example\myapp\MyApp.java'.
    Please post your console session.
    I don't have a console session to post. I just know how to do it. It is your console session I am asking for, without all the extraneous matter. Such everything you have just posted, after I already said 'stop trying all the ones that don't work'.
    I have fixed the makewin.bat file so that it correctly removes and then generates the bin, ext, and obj directories.
    Can we stop for a moment and ask why you aren't using Ant or Maven or an IDE?
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d obj src\com\example\myapp\util\Speak.java
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_class_files -cp obj src\com\example\myapp\MyApp.java
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_jar_file -extdirs ext src\com\example\myapp\MyApp.java
    These are still the wrong command. I've already said I'm not interested in seeing its output. And there is not one instance of the right command. I don't know why you even posted all this stuff. It's of no interest.

    I still don't see anywhere here where you've taken my advice.
  • 1008387
    1008387 Member Posts: 20
    EJP wrote:
    cd src; javac [options] com\example\myapp\MyApp.java
    You're right -- the -extdirs option of javac only seems to work if javac is invoked in the ./src directory. Thanks! :-)

    Here is the tarball with corrections:

    http://holgerdanske.com/users/dpchrist/java/examples/jar-20130515-1919.tar.gz

    Here is the tree:
    2013-05-15 19:21:06 [email protected] ~/sandbox/java/jar
    $ tree -I CVS .
    .
    |-- Makefile
    |-- Makefile.out
    |-- makewin.bat
    |-- makewin.out
    `-- src
        `-- com
            `-- example
                `-- myapp
                    |-- MyApp.java
                    `-- util
                        `-- Speak.java
    
    5 directories, 6 files
    Here are the Java source files:
    2013-05-15 19:21:12 [email protected] ~/sandbox/java/jar
    $ cat src/com/example/myapp/MyApp.java 
    import com.example.myapp.util.Speak;
    public class MyApp {
        public static void main(String [] args) {
    	Speak s = new Speak();
    	s.hello("world!");
        }
    }
    
    2013-05-15 19:21:28 [email protected] ~/sandbox/java/jar
    $ cat src/com/example/myapp/util/Speak.java 
    package com.example.myapp.util;
    public class Speak {
        public static void hello(String arg) {
    	System.out.println("hello, " + arg);
        }
    }
    Here is the Linux Makefile and sample run:
    2013-05-15 19:21:37 [email protected] ~/sandbox/java/jar
    $ cat Makefile
    .PHONY: all
    all: clean using_class_files using_jar_file
    
    .PHONY: using_class_files
    using_class_files:
    	# create utility bytecode file (Speak.class)
    	mkdir -p obj/com/example/myapp/util
    	javac -d obj src/com/example/myapp/util/Speak.java
    
    	# compile application file against Speak.class
    	mkdir -p bin/using_class_files
    	javac -d bin/using_class_files -cp obj src/com/example/myapp/MyApp.java
    
    	# run application
    	java -cp obj:bin/using_class_files MyApp
    
    .PHONY: using_jar_file
    using_jar_file:
    	# put Speak.class into a Java archive (util.jar)
    	mkdir -p ext/com/example/myapp
    	jar cvf ext/com/example/myapp/util.jar -C obj/com/example/myapp/util Speak.class
    
    	# print util.jar table of contents
    	jar tf ext/com/example/myapp/util.jar
    
    	# compile application against util.jar
    	mkdir -p bin/using_jar_file
    	# java must be run from src directory for -extdirs to work (?):
    	cd src; javac -d ../bin/using_jar_file -extdirs ../ext com/example/myapp/MyApp.java
    
    	# run application
    	java -cp ext:bin/using_jar_file MyApp
    
    .PHONY: clean
    clean:
    	rm -rf bin ext obj
    	
    
    2013-05-15 19:21:42 [email protected] ~/sandbox/java/jar
    $ cat Makefile.out 
    rm -rf bin ext obj
    # create utility bytecode file (Speak.class)
    mkdir -p obj/com/example/myapp/util
    javac -d obj src/com/example/myapp/util/Speak.java
    # compile application file against Speak.class
    mkdir -p bin/using_class_files
    javac -d bin/using_class_files -cp obj src/com/example/myapp/MyApp.java
    # run application
    java -cp obj:bin/using_class_files MyApp
    hello, world!
    # put Speak.class into a Java archive (util.jar)
    mkdir -p ext/com/example/myapp
    jar cvf ext/com/example/myapp/util.jar -C obj/com/example/myapp/util Speak.class
    added manifest
    adding: Speak.class(in = 572) (out= 364)(deflated 36%)
    # print util.jar table of contents
    jar tf ext/com/example/myapp/util.jar
    META-INF/
    META-INF/MANIFEST.MF
    Speak.class
    # compile application against util.jar
    mkdir -p bin/using_jar_file
    # java must be run from src directory for -extdirs to work (?):
    cd src; javac -d ../bin/using_jar_file -extdirs ../ext com/example/myapp/MyApp.java
    # run application
    java -cp ext:bin/using_jar_file MyApp
    hello, world!
    Here is the Windows XP batch file and sample run:
    2013-05-15 19:21:48 [email protected] ~/sandbox/java/jar
    $ cat makewin.bat | dos2unix
    REM remove build directories
    rmdir /s /q bin ext obj
    
    REM create utility bytecode file (Speak.class)
    mkdir obj\com\example\myapp\util
    javac -d obj src\com\example\myapp\util\Speak.java
    
    REM compile application file against Speak.class
    mkdir bin\using_class_files
    javac -d bin\using_class_files -cp obj src\com\example\myapp\MyApp.java
    
    REM run application
    java -cp "obj;bin\using_class_files" MyApp
    
    REM put Speak.class into a Java archive (util.jar)
    mkdir ext\com\example\myapp
    jar cvf ext\com\example\myapp\util.jar -C obj\com\example\myapp\util Speak.class
    
    REM print util.jar table of contents
    jar tf ext\com\example\myapp\util.jar
    
    REM compile application against util.jar
    mkdir bin\using_jar_file
    cd src
    javac -d ..\bin\using_jar_file -extdirs ..\ext com\example\myapp\MyApp.java
    
    REM run application
    cd ..
    java -cp "ext;bin\using_jar_file" MyApp
    
    2013-05-15 19:21:56 [email protected] ~/sandbox/java/jar
    $ cat makewin.out | dos2unix
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM remove build directories 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>rmdir /s /q bin ext obj 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM create utility bytecode file (Speak.class) 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir obj\com\example\myapp\util 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d obj src\com\example\myapp\util\Speak.java 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM compile application file against Speak.class 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir bin\using_class_files 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>javac -d bin\using_class_files -cp obj src\com\example\myapp\MyApp.java 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM run application 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>java -cp "obj;bin\using_class_files" MyApp 
    hello, world!
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM put Speak.class into a Java archive (util.jar) 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir ext\com\example\myapp 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>jar cvf ext\com\example\myapp\util.jar -C obj\com\example\myapp\util Speak.class 
    added manifest
    adding: Speak.class(in = 572) (out= 364)(deflated 36%)
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM print util.jar table of contents 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>jar tf ext\com\example\myapp\util.jar 
    META-INF/
    META-INF/MANIFEST.MF
    Speak.class
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>REM compile application against util.jar 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>mkdir bin\using_jar_file 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>cd src 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar\src>javac -d ..\bin\using_jar_file -extdirs ..\ext com\example\myapp\MyApp.java 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar\src>REM run application 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar\src>cd .. 
    
    C:\cygwin\home\dpchrist\sandbox\java\jar>java -cp "ext;bin\using_jar_file" MyApp 
    hello, world!
    David
  • EJP
    EJP Member Posts: 32,919
    edited May 15, 2013 10:51PM
    -extdirs has nothing to do with it. It's for Java extension JARs such as JavaMail, not for more of your code. If you start javac in the directory that is the head of your package structure, it can find and resolve classes imported by the class it is compiling, and indeed compile them if necessary.
  • 1008387
    1008387 Member Posts: 20
    EJP wrote:
    -extdirs has nothing to do with it. It's for Java extension JARs such as JavaMail, not for more of your code.
    So, how do I put libraries into a JAR file and compile against that?

    David
  • EJP
    EJP Member Posts: 32,919
    EJP wrote:
    -extdirs has nothing to do with it. It's for Java extension JARs such as JavaMail, not for more of your code.
    So, how do I put libraries into a JAR file and compile against that?
    You don't. They already are in JAR files. The question doesn't make sense.
  • 1008387
    1008387 Member Posts: 20
    EJP wrote:
    You don't. They already are in JAR files. The question doesn't make sense.
    OK. I will rephrase my question -- how do I put my compiled bytecode (*.class) files into a JAR file and compile (javac) and run (java) against that?

    David
  • 1008387
    1008387 Member Posts: 20
    I have moved this question to a new thread:

    2540700

    David
This discussion has been closed.