GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes CLASSPATH PROBLEM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CLASSPATH PROBLEM" Watch "CLASSPATH PROBLEM" New topic
Author

CLASSPATH PROBLEM

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

Hello ...

I am using ubuntu as operating system and i am using database connections in my application ...

so i have set CLASSPATH for java.sql connector package and works fine..

Now when i compile the entire package and put that all classes in one jar file then it shows me error regarding it didn't find any database class that i used of java.sql package

command line invocation

javac -d RetailClasses/ Retail/*.java
//jar creation
jar -cf ../RetailProgram.jar Retail
//running app

java -cp ../RetailProgram.jar:. Retail.RetailCompany

Class not found :-com.mysql.jdbc.Driver
Exception in thread "main" java.lang.NullPointerException
at Retail.DBConnection.selectRecords(DBConnection.java:80)
at Retail.RetailCompany.<init>(RetailCompany.java:402)
at Retail.RetailCompany.main(RetailCompany.java:422)

Now my question is i have set CLASSPATH setting for java.sql package in bash.bashrc file in ubuntu and i am providing another classpath at command line invocation .Will it override my system CLASSPATH because it works perfectly if i remove this jar files and then run like simple program..

Kindly clear this ..

Thanks in advance....


OCPJP(83%)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ankur trapasiya wrote:Will it override my system CLASSPATH because it works perfectly if i remove this jar files and then run like simple program..

what do you think?I think exception as you mentioned above. hmm.. what about the exception of *com.mysql.jdbc.Driver* ?do you have the jar in your class path?
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

mysql.jar for jdbc is in CLASSPATH . which i have set in environment variable CLASSPATH ...

and another classpath of jar file i am giving as command line parameter...
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

ankur trapasiya wrote:mysql.jar for jdbc is in CLASSPATH . which i have set in environment variable CLASSPATH ...

and another classpath of jar file i am giving as command line parameter...


I think it takes the Classpath provided in the command line and ignores the one set in the CLASSPATH variable. Dont know the behavior of it in Ubuntu though.


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14190
    
  20


A couple of things about the classpath:

  • When you use the -cp or -classpath switch on the command line, the CLASSPATH environment variable will be ignored. Java will not automatically use what you put on the command line and the CLASSPATH environment variable both at the same time.
  • When you use the -jar switch to run your program as an executable JAR, then the CLASSPATH environment variable will be ignored, and also the -cp and -classpath switches will be ignored. The place to set your classpath in that case is in the META-INF/MANIFEST.MF file (using the Class-Path attribute).


  • See Packaging Programs in JAR Files in The Java Tutorials.

    So, it does not work if you put the JDBC driver in the CLASSPATH environment variable, but also use the -cp switch on the command line. Put the JDBC driver also in the path that you use after the -cp switch.

    Setting CLASSPATH or using -cp or -classpath on the command line when you also use the -jar switch will also not work.

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Wouter Oet
    Saloon Keeper

    Joined: Oct 25, 2008
    Posts: 2700

    Mohamed Sanaulla wrote:I think it takes the Classpath provided in the command line and ignores the one set in the CLASSPATH variable. Dont know the behavior of it in Ubuntu though.
    The behaviour is the same on all platforms. Java is platform independent and this is standardised.


    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
    Please correct my English.
    Mohamed Sanaulla
    Saloon Keeper

    Joined: Sep 08, 2007
    Posts: 3071
        
      33

    Wouter Oet wrote:
    Mohamed Sanaulla wrote:I think it takes the Classpath provided in the command line and ignores the one set in the CLASSPATH variable. Dont know the behavior of it in Ubuntu though.
    The behaviour is the same on all platforms. Java is platform independent and this is standardised.

    Oh yeah. Didn't realize that though. Edit: Thanks for pointing it out
    Wouter Oet
    Saloon Keeper

    Joined: Oct 25, 2008
    Posts: 2700

    I would recommend you not to use the classpath environment variable because:

    If you are working on multiple projects at the same time you'll have you alter it every time you switch projects.
    If you want to run 2 programs which both use the variable at the same time you'll have to create 2 different session in which they run.
    And it's not really portable (often you need administrator rights to set that variable).

    Just stick with -cp.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39044
        
      23
    Please avoid writing in UPPER-CASE.,
    Javin Paul
    Ranch Hand

    Joined: Oct 15, 2010
    Posts: 281


    Hi Ankur,

    Its worth noticing that how you run your program finally i.e. by using -jar switch or simply with providing classname as suggested by "Jesper de Jong" that in case of -jar option CLASSPATH environment variable and -cp option will be ignored and classpath defined in the Manifest file will be take over.

    Thanks
    Javin


    http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
    ankur trapasiya
    Ranch Hand

    Joined: Sep 24, 2010
    Posts: 160

    so what to do now ??? i have to path of both jar files in command line arguments ??? Is it a good solution ???
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14190
        
      20

    ankur trapasiya wrote:so what to do now ??? i have to path of both jar files in command line arguments ??? Is it a good solution ???

    Yes, did you try it? If you don't want to type that whole path each time you run the program, you can put the command in a shell script.

    Create a file named run.sh that contains the following text:

    Make the script executable: chmod u+x run.sh
    Then you can run it like this: ./run.sh
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: CLASSPATH PROBLEM