Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ultimateceej

macrumors regular
Original poster
Jul 24, 2007
176
0
Texas
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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:
Code:
jar cvf MyCoolLib.jar myClass.class myClass2.class

-Lee
 

ultimateceej

macrumors regular
Original poster
Jul 24, 2007
176
0
Texas
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors regular
Original poster
Jul 24, 2007
176
0
Texas
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors regular
Original poster
Jul 24, 2007
176
0
Texas
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

macrumors regular
Original poster
Jul 24, 2007
176
0
Texas
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:
Code:
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.