Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Importing Java classes

Importing Java classes
Thread Tools
Dimitri
Junior Member
Join Date: Mar 2004
Location: St. Paul, MN
Status: Offline
Reply With Quote
Jul 18, 2004, 11:33 PM
 
There's a java class I have called KeyIn.class. Where do I install this so I can import it from a Java file from anywhere?

Thanks.
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jul 19, 2004, 09:40 AM
 
Originally posted by Dimitri:
There's a java class I have called KeyIn.class. Where do I install this so I can import it from a Java file from anywhere?
You just need to put it in your classpath. By default your classpath includes ~/Library/Java/Extensions so you could put it there.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Dimitri  (op)
Junior Member
Join Date: Mar 2004
Location: St. Paul, MN
Status: Offline
Reply With Quote
Jul 19, 2004, 09:51 AM
 
Thanks.
So if I put the class under \Library\Java\Extensions\psJava I could use the line:
import psJava.KeyIn; to use it?

Thanks again.
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Jul 19, 2004, 02:11 PM
 
Originally posted by Dimitri:
Thanks.
So if I put the class under \Library\Java\Extensions\psJava I could use the line:
import psJava.KeyIn; to use it?

Thanks again.
Backslashes are a DOS thing. You use forward slashes in UNIX. You'll note in my post I said ~/Library, not /Library. In UNIX, ~/ refers to your home directory.

If the java file is in the package psJava, then you could install the psJava folder in /Users/yourusername/Library/Java/Extensions/psJava and reference it as "import psJava. KeyIn;" if you wish to do so.

The better solution if you have multiple classes is to create a jar file, and put that jar file in the aforementioned Extensions folder.

You could also just put it wherever you like and modify the CLASSPATH environment variable to include that directory in your classpath. That's what I do.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Dimitri  (op)
Junior Member
Join Date: Mar 2004
Location: St. Paul, MN
Status: Offline
Reply With Quote
Jul 19, 2004, 02:22 PM
 
Ok, I got it.
If I created a jar file and named it psJava.jar in the extensions folder, I could then import it using import psJava.KeyIn; I take it?

One last thing, how does one modify the CLASSPATH variable?

Thanks for your help.
     
Dimitri  (op)
Junior Member
Join Date: Mar 2004
Location: St. Paul, MN
Status: Offline
Reply With Quote
Jul 19, 2004, 07:36 PM
 
Hmmm, I can't seem to compile my java file with the import command. Keeps saying it can't find the package. I created the Java\Extensions folder under the ~\Library\ folder and have tried multiple ways and can't seem to get it to work. Even setting the CLASSPATH in my compiler doesn't work.
     
itistoday
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 19, 2004, 11:14 PM
 
Originally posted by Dimitri:
Hmmm, I can't seem to compile my java file with the import command. Keeps saying it can't find the package. I created the Java\Extensions folder under the ~\Library\ folder and have tried multiple ways and can't seem to get it to work. Even setting the CLASSPATH in my compiler doesn't work.
Generally when dealing with simple classes and not libraries conatined in Jars you just want to put it in the directory that contains your main() method. i.e. if your file called Face.java has the main() method, just put it in that folder and call the class directly without importing it.

Edit: If it's part of a package, say "face.crap", then what you need to do is create the directory tree. In the folder that holds your Face.java file with its main() method, create the folders 'face/crap'. Put your class that's giving you trouble into the crap folder (if you have the source, compile it inside that folder). Then, open the terminal, and go to the folder that contains Face.java. Type in 'jar -cf face.jar face'. This will make a file called face.jar in this directory. Next, in your Face.java file, import the package 'face.crap.*'. Then, compile your program by typing in 'javac -classpath face.jar Face.java'.

And it should work.
( Last edited by itistoday; Jul 20, 2004 at 12:33 AM. )
     
itistoday
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 20, 2004, 12:34 AM
 
If you read the my previous reply and tried it, you probably found that it didn't work. But I have edited it now and it should work.
     
Dimitri  (op)
Junior Member
Join Date: Mar 2004
Location: St. Paul, MN
Status: Offline
Reply With Quote
Jul 20, 2004, 12:42 AM
 
Yeah, that works. I guess what I wanted, however, was the convenience of importing the class from any directory, like I do with the javax.swing package. Thus, the rest of my java programs I write I won't have to go through all that work. It's not worth it in that case.
     
itistoday
Professional Poster
Join Date: Oct 2001
Status: Offline
Reply With Quote
Jul 20, 2004, 12:52 AM
 
Originally posted by Dimitri:
Yeah, that works. I guess what I wanted, however, was the convenience of importing the class from any directory, like I do with the javax.swing package. Thus, the rest of my java programs I write I won't have to go through all that work. It's not worth it in that case.
Well, it should still work if you take that jar file you just made and put it in ~/Library/Java/Extensions/. Then just compile it normally. But if you do that then you won't be able to distribute your application to other people.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:44 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,