Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CLASSPATH PROBLEM

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
     
    Wouter Oet
    Saloon Keeper
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Mohamed Sanaulla
    Saloon Keeper
    Posts: 3159
    33
    Google App Engine Java Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2700
    IntelliJ IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49367
    62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please avoid writing in UPPER-CASE.,
     
    Javin Paul
    Ranch Hand
    Posts: 295
    Eclipse IDE Firefox Browser Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
     
    ankur trapasiya
    Ranch Hand
    Posts: 160
    Java Netbeans IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 15354
    39
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic