User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Proper way to create a complete java package Page [1]  
aaronburro
Sup, B
52854 Posts
user info
edit post

Alright, no flaming here. I could sit back and take a few hours to figure this out via the sometimes cryptic java documentation, but I really don't have that kinda time to spare. Anywho, I have a bunch of classes from a textbook that I would like to make into a package. I used the package keyword and named them appropriately, but I am doing it wrong, as the thing isn't working. Yes, I've tried compiling while setting the classpath and it just aint working the way I would expect. I tried making a jar file and sticking it in the lib\whatever\ext directory of the JRE, but that don't work either. So how am I supposed to accomplish this? I'd rather not just copy the class files that I need for this one project into the project directory. plus, I have a namespace duplication as well, which makes it even more of a pain in the ass. what do I do?

9/28/2005 10:42:07 PM

aaronburro
Sup, B
52854 Posts
user info
edit post

bttt

9/29/2005 8:32:49 AM

MiniMe_877
All American
4414 Posts
user info
edit post

javac to compile it
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html

and use jar to package it into a .JAR file
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/jar.html

then use java to run it
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

9/29/2005 9:05:50 AM

aaronburro
Sup, B
52854 Posts
user info
edit post

hey, thx for the basic advice. I'm glad to know that I am proceeding on the right course.

anyway, how would I then utilize the package?

9/29/2005 9:35:23 AM

ioeth
Starting Lineup
61 Posts
user info
edit post

I'm not totally sure how you have everything setup, aaronburro, so I'll just try to hit all of the main points of compiling a package.

Let's say that you have a "src" dir that contains all of your source files. You have to remember that within this directory, that there must be subdirectories that match the package definition to contain your Java source files. That is to say, if you have a class "myclass.java" with "package edu.ncsu.csc" at the top, then within your "src" directory, you would need to have "MyClass.java" in "src/edu/ncsu/csc/MyClass.java" for the compiler to do its job. Once all of your files match this format, you can do a "javac" on the "src" directory itself. It will recurse into the subdirectories and compile all of your files.

You may want to redirect the output of the compile into another directory in order to make it easier to JAR your files up. You would do this by using the "-d directory". Your "javac" command should look something like "javac -d ./bin ./src".

Once you've done that, you can JAR the whole thing up. If you output the compiled classes to the "bin" directory, then you can use do that by entering "jar cvf myjar.jar -C bin/ ." The "c" specifies that you want to create an archive, the "v" signals for verbose output, the "f" allows you to specify the "myjar.jar" file, and the "-C" signals that you want to run this from within the "bin" directory. Fianlly, the "." tells JAR to include everything within the "bin" directory.

If you want to make your JAR file executable, then you need to create a text file named "MANIFEST.MF" (in the same directory that you are running the JAR command from, preferably). This file needs to only have one line in it which reads "Main-Class: edu.ncsu.csc.MyClass" or whatever class within the jar has a "main" method. Then you will need to alter your JAR command to specify the manifest file. It would then read "jar cvf ./MANIFEST.MF myjar.jar -C bin/ ."

If you get any error messages, post them and I'll see what I can do to help.

9/29/2005 9:35:58 AM

ioeth
Starting Lineup
61 Posts
user info
edit post

Alternatively, you can use Apache Ant to do all of this for you with an XML script.

http://ant.apache.org/

9/29/2005 9:45:53 AM

psnarula
All American
1540 Posts
user info
edit post

yeah i was gonna say load all the files into Eclipse and then run Ant and it'll jar it all up for you. of course, you have to write your own ant script but there are tutorials on the internet for how to do this.

9/29/2005 11:29:03 AM

Shaggy
All American
17820 Posts
user info
edit post

i still prefer just using jar.exe from the commandline.

if i need to repack something i have a script that does

jar uf .jar *.class
jarsigner .jar mykey

This updates the jar with the latest version of the class files and then signs it with my self cert key.

the keysigning is so that it can be run from java web start since it read/writes on the local system

9/29/2005 11:50:08 AM

aaronburro
Sup, B
52854 Posts
user info
edit post

hey, thx ioeth. after a little more trial and error i was able to get it to work. then it was just a matter of copying the jar file into an ext directory somewhere and that was that. thx again!

9/29/2005 11:54:42 AM

aaronburro
Sup, B
52854 Posts
user info
edit post

ok, one more thing.

how do I deal w/ namespace problem within the package? For instance, one of the classes in the package is named List and is located in CSC316F05 (the root of the package). how do I make the stuff inside the package refer to the package's instance of List without having to type CSC316F05.List?

9/29/2005 1:16:13 PM

ioeth
Starting Lineup
61 Posts
user info
edit post

You can simply add "import CSC316F05.List;" to the top of the package classes and then you can refer to the class as "List" rather than "CSC316F05.List".

9/29/2005 1:37:49 PM

 Message Boards » Tech Talk » Proper way to create a complete java package Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.