Results 1 to 3 of 3
  1. #1
    zhacker is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Adding .jar archive to classpath with javac

    Hi everyone,

    I have been programming in java for a while but I haven't used external .jar archives previously. Now, I am faced with the current situation:


    1- I have a java file (SNPHarvester.java) located in my /home directory.
    2- Under: /home/lib/ I have two .jar archives, namely: commons-math-1.2.jar and weka.jar
    3- In the sourcecode (SNPHarvester.java), I have the following import statements:

    import weka.core.Utils;

    import org.apache.commons.math.distribution.ChiSquaredDis tribution;
    import org.apache.commons.math.distribution.ChiSquaredDis tributionImpl;


    Now:

    my weka.jar has the structure: weka/core/Utils.class
    my commons-math-1.2.jar contains the structure: /org/apache/commons/math/distribution/ChiSquaredDistribution.class and ChiSquaredDistributionImpl.class - However, notice the name of the .jar archive - unlike my weka.jar, commons-math-1.2 does not show up in the import statement.


    Now, when I compile using the following:

    javac -cp ".:lib/weka.jar:lib/commons-math-1.2.jar" SNPHarvester.java it outputs the following:

    [zibrahimbrc@bignode lib]$ javac -cp ".:lib/weka.jar:lib/commons-math-1.2.jar" SNPHarvester.java
    javac: file not found: SNPHarvester.java
    Usage: javac <options> <source files>
    use -help for a list of possible options


    Which I am assuming it's okay. However, the following exception is generated when I run the corresponding .class file:

    [zibrahimbrc@bignode snp_harvester]$ java SNPHarvester
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math/distribution/ChiSquaredDistribution
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.math.distribution.ChiSquaredDis tribution
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    Could not find the main class: SNPHarvester. Program will exit.


    It's not finding the classes in commons-math-1.2.jar


    Any ideas about what I'm doing wrong? your help is highly appreciated. Thank you very much in advance.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Adding .jar archive to classpath with javac

    Quote Originally Posted by zhacker View Post
    javac -cp ".:lib/weka.jar:lib/commons-math-1.2.jar" SNPHarvester.java it outputs the following:

    [zibrahimbrc@bignode lib]$ javac -cp ".:lib/weka.jar:lib/commons-math-1.2.jar" SNPHarvester.java
    javac: file not found: SNPHarvester.java
    Usage: javac <options> <source files>
    use -help for a list of possible options
    That hasn't compiled.
    It can't see your java file.
    What directory are you in when you run the javac command?

    Quote Originally Posted by zhacker View Post
    Which I am assuming it's okay. However, the following exception is generated when I run the corresponding .class file:

    [zibrahimbrc@bignode snp_harvester]$ java SNPHarvester
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math/distribution/ChiSquaredDistribution
    Caused by: java.lang.ClassNotFoundException: org.apache.commons.math.distribution.ChiSquaredDis tribution
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    Could not find the main class: SNPHarvester. Program will exit.


    It's not finding the classes in commons-math-1.2.jar


    Any ideas about what I'm doing wrong? your help is highly appreciated. Thank you very much in advance.
    Not sure why you;re getting the first part about the commons stuff as it then says it can't find the SNPHarvester.

    ANyway, the java command will need the -cp switch as well, since it needs those two jars to run.

  3. #3
    zhacker is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: Adding .jar archive to classpath with javac

    Quote Originally Posted by Tolls View Post
    That hasn't compiled.
    It can't see your java file.
    What directory are you in when you run the javac command?
    Sorry, I pasted the wrong output :) ...the compilation was fine.



    Quote Originally Posted by Tolls View Post
    Not sure why you;re getting the first part about the commons stuff as it then says it can't find the SNPHarvester.

    ANyway, the java command will need the -cp switch as well, since it needs those two jars to run.
    Thank you very much. That did it! :) I really appreciate your reply.

Similar Threads

  1. Replies: 5
    Last Post: 06-02-2011, 06:59 AM
  2. Replies: 8
    Last Post: 09-23-2010, 03:27 PM
  3. Adding jar to classpath.
    By dudejonne in forum New To Java
    Replies: 11
    Last Post: 11-04-2009, 04:58 PM
  4. Replies: 3
    Last Post: 08-14-2009, 12:57 PM
  5. Adding JARs to the classpath at runtime?
    By johann_p in forum Advanced Java
    Replies: 1
    Last Post: 06-26-2007, 10:42 PM

Posting Permissions

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