Results 1 to 4 of 4
  1. #1
    jfredrickson is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

    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 09:45 AM.

  3. #3
    jfredrickson is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    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:

    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:

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

    Java 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?

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    10

    Default

    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.

    Java 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?)!

Similar Threads

  1. Need help with importing classes
    By Deathmonger in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:03 AM
  2. Importing / compiling and running with .jar package
    By splinter64uk in forum New To Java
    Replies: 1
    Last Post: 12-05-2007, 02:47 AM
  3. Importing package
    By bugger in forum New To Java
    Replies: 5
    Last Post: 11-26-2007, 12:29 PM
  4. Importing classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-06-2007, 02:27 PM
  5. Exporting/Importing JAR files
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •