LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-16-2012, 05:43 PM   #1
kruger53
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Rep: Reputation: Disabled
Setting the CLASSPATH


Yes I'm sorry I'm a newbie. I've probably missed something really obvious but I'm stuck. Can anyone please tell me why java will not use the CLASSPATH when I set it as environment var?

I'm using Mint Maya, JEE6 and JDK 1.7.07

I set my classpath by
CLASSPATH=".:/opt/sqlanywhere12/java/jconn3.jar"

I confirm it is set by
mark@mark-GA-880GM-USB3 ~/jwork/jwork/jdbc $ echo $CLASSPATH
.:/opt/sqlanywhere12/java/jconn3.jar


I then run my test connection program that I 'borrowed' from a blog (listing below) and get. Cause I suspect by java not finding the CLASSPATH

mark@mark-GA-880GM-USB3 ~/jwork/jwork/jdbc $ java ShowConnect
---Preloaded drivers---
No drivers listed as jdbc.drivers property.
The drivers listed as being loaded:
sun.jdbc.odbc.JdbcOdbcDriver@1d35bf2
---Loading driver---
java.lang.ClassNotFoundException: com.sybase.jdbc3.jdbc.SybDriver
The drivers listed as being loaded:
sun.jdbc.odbc.JdbcOdbcDriver@1d35bf2
---Making connection---
java.sql.SQLException: No suitable driver found for jdbc:sybase:Tds:localhost:2638/jobbeat.db
Connection failed.
Exception in thread "main" java.lang.NullPointerException
at ShowConnect.main(ShowConnect.java:33)


However if I run the program with the classpath as a command line parameter then it works fine.

mark@mark-GA-880GM-USB3 ~/jwork/jwork/jdbc $ java -cp .:/opt/sqlanywhere12/java/jconn3.jar ShowConnect
---Preloaded drivers---
No drivers listed as jdbc.drivers property.
The drivers listed as being loaded:
sun.jdbc.odbc.JdbcOdbcDriver@1d35bf2
---Loading driver---
The drivers listed as being loaded:
sun.jdbc.odbc.JdbcOdbcDriver@1d35bf2
com.sybase.jdbc3.jdbc.SybDriver@a8a271
---Making connection---
com.sybase.jdbc3.jdbc.SybConnection@161eccb
Database product name: SQL Anywhere
Database product version: 12.0.1.3759
Driver name: jConnect (TM) for JDBC (TM)
Driver version: jConnect (TM) for JDBC(TM)/6.05(Build 26685)/P/EBF19405/JDK14/Sat Aug 6 21:33:11 2011


Program listing
import java.util.Properties;
import java.util.Enumeration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.DatabaseMetaData;

public class ShowConnect {
private static final String DRIVER_CLASS_NAME =
"com.sybase.jdbc3.jdbc.SybDriver";
private static final String DATABASE_URL =
"jdbc:sybase:Tds:localhost:2638/userdb.db";
private static final String USERNAME = "admin";
private static final String PASSWORD = "***";

public static void main(String arg[]) {
ShowConnect showcon = new ShowConnect();

System.out.println(" ---Preloaded drivers---");
showcon.listPropertyDrivers();
showcon.listLoadedDrivers();

System.out.println(" ---Loading driver---");
showcon.loadDriver(DRIVER_CLASS_NAME);
showcon.listLoadedDrivers();

System.out.println(" ---Making connection---");
Connection connection = showcon.getConnection(
DATABASE_URL,USERNAME,PASSWORD);
showcon.showConnection(connection);
try {
connection.close();
} catch(SQLException e) {
System.out.println(e);
}
}
public void listPropertyDrivers() {
Properties properties = System.getProperties();
String str = properties.getProperty("jdbc.drivers");
if(str == null)
System.out.println("No drivers listed as jdbc.drivers property.");
else
System.out.println("Property jdbc.drivers: " + str);
}
public void listLoadedDrivers() {
Enumeration enumeration = DriverManager.getDrivers();
if(enumeration.hasMoreElements())
System.out.println("The drivers listed as being loaded:");
else
System.out.println("No drivers listed as being loaded.");
while(enumeration.hasMoreElements())
System.out.println(" " + enumeration.nextElement());
}
public void loadDriver(String driver) {
try {
Class.forName(driver);
} catch(ClassNotFoundException e) {
System.out.println(e);
}
}
public Connection getConnection(String url,String user,String password) {
try {
Connection connection = DriverManager.getConnection(
url,user,password);
return(connection);
} catch(SQLException e) {
System.out.println(e);
}
return(null);
}
public void showConnection(Connection connection) {
if(connection == null) {
System.out.println("Connection failed.");
return;
}
System.out.println(connection);
try {
DatabaseMetaData meta = connection.getMetaData();
System.out.println(" Database product name: " +
meta.getDatabaseProductName());
System.out.println(" Database product version: " +
meta.getDatabaseProductVersion());
System.out.println(" Driver name: " +
meta.getDriverName());
System.out.println(" Driver version: " +
meta.getDriverVersion());
} catch(SQLException e) {
System.out.println(e);
}
}
}
 
Old 09-16-2012, 05:49 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
You have to export it, otherwise it's just a shell variable not an environment variable. You can use env to confirm it:
Code:
~$ CLASSPATH=foo
~$ env | grep CLASSPATH
~$ export CLASSPATH
~$ env | grep CLASSPATH
CLASSPATH=foo
PS please use [CODE] [/CODE] tags around code listings.

Last edited by ntubski; 09-16-2012 at 05:50 PM. Reason: missed word
 
1 members found this post helpful.
Old 09-16-2012, 05:57 PM   #3
kruger53
LQ Newbie
 
Registered: Sep 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
You have to export it, otherwise it's just a shell variable not an environment variable. You can use env to confirm it:
Code:
~$ CLASSPATH=foo
~$ env | grep CLASSPATH
~$ export CLASSPATH
~$ env | grep CLASSPATH
CLASSPATH=foo
PS please use [CODE] [/CODE] tags around code listings.
Brilliant. Thank you very much.

And I will use the code tags in future.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
setting classpath in ubuntu jayrmca Linux - Software 2 03-25-2009 08:08 AM
setting path and classpath Raakh Linux - Newbie 4 07-10-2007 02:39 AM
setting classpath and javahome in fedora 5 me4linux Programming 7 03-07-2007 07:54 PM
setting java classpath... brutal_brad Linux - Newbie 7 09-21-2003 03:52 PM
setting CLASSPATH variable gabadoo Linux - Newbie 7 01-21-2002 05:24 PM


All times are GMT -5. The time now is 08:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration