PDA

View Full Version : using user defined java packages




ultimateceej
Jul 1, 2008, 04:18 PM
I want to use some classes I have created and make them into a package.
I already have to package statement but how do I make other programs that need to import my package find it.



lee1210
Jul 1, 2008, 04:42 PM
Traditionally you would build a jar file and distribute it. In a project that wanted to utilize your code, the jar file would be added to the classpath for builds and to run this program. Most IDEs allow you to generate a jar file automatically. Otherwise you can run:
jar cvf MyCoolLib.jar myClass.class myClass2.class

-Lee

ultimateceej
Jul 1, 2008, 04:47 PM
Couldn't I move my package to the default classpath that is already set by Java? I've been looking around my computer but I can't find the folders that contain the packages included with Java, like util and swing. Do you know where that is? I'm using Tiger... I'm not sure if that makes a difference. Thanks.

lee1210
Jul 1, 2008, 04:52 PM
I misunderstood completely, i thought you were wanting to distribute this class for others to use. I would avoid using the system classpath. Just set your classpath to the location of the class(es) that you want to use. Otherwise, I am not at my Mac so I can't help much on finding where the default classes are.

-Lee

ultimateceej
Jul 1, 2008, 09:47 PM
Why do you recommend not using the system path? Wouldn't it be just as easy to move all my classes there?

I've been searching around for the default classpath and found the echo command that will tell me the default but I don't see any java packages.

lee1210
Jul 1, 2008, 10:45 PM
I guess it's just a matter of taste. If you get used to using jars, that will work if you want to share your program, or just move it to another machine. It saves you from having to do system specific setup, which is one of the things Java strives towards.

I'll check out where the defaults are on my tiger system in a few minutes for the global CP.

-Lee

lee1210
Jul 1, 2008, 11:14 PM
I just did a test on my system, and it looks like you could place your jars or classes in:
/System/Library/Java/Extensions

I don't know where this is configured. This page mentions this, but is somewhat vague:
http://java.sun.com/docs/books/tutorial/ext/basics/load.html

I would note that this may be different on your system. I built a class, placed it in that directory, and it was picked up by javac and java when I compiled and ran another class that made use of the original. I still think jar'ing and/or using the CLASSPATH environment variable to define your own classes is better than mucking with the system things which are subject to change from version to version or system to system.

-Lee

ultimateceej
Jul 2, 2008, 12:54 PM
Thanks for the help. I found all the predefined classes in a jar file and now I know what you mean by "mucking" with the system! At least I have a better understanding of jar files and packages now. Thanks. :)

ultimateceej
Jul 2, 2008, 01:04 PM
Traditionally you would build a jar file and distribute it. In a project that wanted to utilize your code, the jar file would be added to the classpath for builds and to run this program. Most IDEs allow you to generate a jar file automatically. Otherwise you can run:
jar cvf MyCoolLib.jar myClass.class myClass2.class

-Lee

The jar manual for the v command says verbose output to stderr. What's that???

lee1210
Jul 2, 2008, 01:31 PM
This means you will get detailed output about the functions performed by the command. it is not necessary, and the result will be the same if you do not include it. I personally find it comforting to see all the files added listed there, etc.

-Lee

AlmostThere
Jul 2, 2008, 05:41 PM
Why do you recommend not using the system path? Wouldn't it be just as easy to move all my classes there?

One issue with this is that different programs might need different versions of the same library, potentially leading to instability.

-

Three additional options spring to mind:

What you can also do is specify a classpath in your application jar's manifest file. This will let your application look in a relative or pre-defined directory for library jars.

Another option is to use the Jar Bundler tool (/Developer/Applications/Java Tools/Jar Bundler.app). This will turn your application jar into a Mac .app and allow you to add library jar files into the package.

A third suggestion, if you use Eclipse, is to download a tool called FatJar. This unbundles the classes from library jar files and wraps the whole lot as a single jar file (similar tools are available for other IDEs, although I have had minimal success with Jar Tool in IntelliJ).

ultimateceej
Jul 3, 2008, 01:30 AM
Thanks for all the help!