Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-11-2007, 11:02 AM
Member
 
Join Date: Jul 2007
Posts: 4
jfredrickson is on a distinguished road
Importing a Custom Class
I downloaded a custom class and would like to use it in my program, but I can't figure out how to import it. It came as a packaged jar file. Where do I place this file, and what is the correct syntax for importing custom classes like this?

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-11-2007, 11:36 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
You should find out what package those classes are in. If this is not written somewhere else then open the jar file with winrar/winzip. (Or extract it with jar command line tool comes with JDK)

Check which directory is your class in. That will directly be the package name!

For example if the classes you want to use is in org/mypackage/anotherone, then you whould import those classes from your java file like this:

import org.mypackage.anotherone.*;

This will import the classes in org.mypackage.anotherone package.

Quote:
Where do I place this file
You need to add this JAR to your classpath.

If you are using an IDE and already have a project, you can add this jar to the project's classpath. This is generally available in your project's properties (In NetBeans, right click on your project, select Properties and add the JAR to your classpath).

If you don't use an IDE, you have two choices left. One of them is to add command line parameters to your java and javac commands. Write "javac -help". You will see a CLASSPATH option there. So you need to specify the path of your JAR file as the classpath. I recommend you to place the jar to the same place of your java class and use "java -cp . yourClass.java" simply.

And alternatively, although it is not recommended, you can place this jar to your jre's lib/ext directory. Since that place is automatically added to your classpath variable, your JAR will be find if you place it there.

Check this resource for more detailed information:

Classpath (Java - Wikipedia, the free encyclopedia)

Last edited by JavaBean : 07-11-2007 at 11:45 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-11-2007, 12:07 PM
Member
 
Join Date: Jul 2007
Posts: 4
jfredrickson is on a distinguished road
Sorry, I'm a bit new to importing classes. I put the jar file into a folder and created a test.java file in the same folder. Then I put the following code in test.java:

Code:
import java.lang.ocr.jar; class ragnar { public static void main(String args[]) { System.out.println("It's working!"); } }
The documentation for the OCR class said that it is a subclass of java.lang.Object, but I don't think I got that import statement correct. I tried executing it like this:

Code:
java -cp . test.java
But it returned the following error:

Code:
Exception in thread "main" java.lang.NoClassDefFoundError: test/java
I am not using an IDE, and I'd like to be able to just do this all from the terminal (I'm on Mac OSX) if that's possible. What else can I try to get this to work?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-11-2007, 01:23 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Ok. this is more clear now. And your setting is the most simplest one which will simplify solving your problem.

You only need to find the package of the class.

As i said before, you need to extract jar file and detect the structure. In the command line run the following command in the folder where you have the jar.

Code:
jar -xvf *.jar
And then tell me the structure of the extracted folders (Where are the classes? Which directories and sub-directories are created?)!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with importing classes Deathmonger New To Java 3 02-07-2008 12:03 PM
Importing / compiling and running with .jar package splinter64uk New To Java 1 12-05-2007 04:47 AM
Importing package bugger New To Java 5 11-26-2007 02:29 PM
Importing classes Java Tip Java Tips 0 11-06-2007 04:27 PM
Exporting/Importing JAR files JavaForums Eclipse 0 04-26-2007 12:15 PM


All times are GMT +3. The time now is 10:54 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org