LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-31-2009, 05:57 AM   #1
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,168
Blog Entries: 23

Rep: Reputation: 275Reputation: 275Reputation: 275
Question Classpath - quick question


Hi all,

Just a quickie, if I set the classpath in the environment, I'd use

Quote:
>export set CLASPATH=/da/path/to.this:$CLASSPATH
There are two things here:

- how do I see what the classpath is BEFORE I set it, in case I s*** something up and need to set it back
- how do I edit it, in case I need to...

Tnx!

BTW, I know that I can see the whole bundle with

Quote:
> set | more
but Classpath does'nt show, even thoug Java is installed, I assume that this means it's simply not set (yet)...
 
Old 03-31-2009, 08:34 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 40
Like all environment variables, CLASSPATH won't be set unless you (or some other script) sets it. Do `echo $CLASSPATH` to see it's value. Also note that your method works for appending to an existing classpath but if it doesn't exist yet you get an extra colon at the end (I'm 99.99% sure Java would discard this, but following the rule that you should be liberal about what you accept and strict about what you emit, it might be good to do it right; detect whether it's set or not first, e.g. `if [ "X${CLASSPATH}" = "X" ]` and then either set it straight up or append it.)
 
Old 03-31-2009, 08:41 AM   #3
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 65

Rep: Reputation: 17
Quote:
Originally Posted by linusr@flanders View Post
- how do I see what the classpath is BEFORE I set it, in case I s*** something up and need to set it back
just type echo $CLASSPATH on a shell.

Quote:
Originally Posted by linusr@flanders View Post
- how do I edit it, in case I need to...
When you set the classpath the previous setting is overwritten. In this case you added a path keeping the previous ones because you included $CLASSPATH at the end. However you may type something like this:
Code:
export CLASSPATH=/da/path/to/this:/some/other/path
I hope this answers your question.

Quote:
Originally Posted by linusr@flanders View Post
Classpath does'nt show, even thoug Java is installed, I assume that this means it's simply not set (yet)...
Right, it won't be set unless you do it explicitly. Setting the classpath as an exported environment variable in a shell is usually a bad idea since there is no universal setting for all java programs. It is usually set by the script that launches a java program. Of course there are exceptions, it depends on your needs. Are you trying to run a particular application?
 
Old 03-31-2009, 04:42 PM   #4
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,168
Blog Entries: 23

Original Poster
Rep: Reputation: 275Reputation: 275Reputation: 275
Hi, and tnx 4 the replies.

The aim is to make a connector to a mysql database. I downloaded ConnectorJ (from mysqll.com themselves) and now need to - somehow - get the jar into the game.

Of course, and this struck me at work - long after posting this thread, I need to set the exact path to the jar (the driver is packed as jar) on the server and the dev-machine. Then, I'll have to import the driver - of course. Somehow.

This is the snippet I picked up from the install-and-config guide:

Quote:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
The code fails with a nosuchdevfoundexception...possibly due to the fact that the classpath is'nt set (properly)...
Of course, the fact that it' an Exception, means the JVM is already up and running...

So there, that's the quandry...

Thor
 
Old 04-01-2009, 03:08 AM   #5
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 65

Rep: Reputation: 17
nosuchdevfoundexception??? Never heard about it before, not even google knows about it . Did you mean NoClassDefFoundError or ClassNotFoundException?
 
Old 04-01-2009, 03:51 AM   #6
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,168
Blog Entries: 23

Original Poster
Rep: Reputation: 275Reputation: 275Reputation: 275
Erhm...yes, what you said, it was NoClassDefFoundError - and (in my humble defense) the reply was written @ 1 in the morning, after I got back from work, and just had a shower. Sorry

In case you're interested, this is what I got back
Quote:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver.class
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
Caused by: java.lang.ClassNotFoundException: LoadDriver.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
And I conclude that this either means that the import was'nt done, even though the comment in the code stated not to do it (weird as far as I'm concerned) or the JVM does'nt find the jar (located in /usr/DBConns/ConnectorJ/) hence: CLASSPATH set wrong, but when I call up the classpath, it seems correct...

(time lapsed...)

Did a check, the classpath is set correctly. Yesterday, I had a colon at the end, did'nt work (big tnx taylor venable) - so I possibly dont address the package correctly.
By the way, the CLASSPATH was un-set (aka empty) this morning, so I've possible forgotten to save it - somehow...

This the digest from the run with the correct CLASSPATH...

Quote:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver.class
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
Caused by: java.lang.ClassNotFoundException: LoadDriver.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/usr/DBConns/ConnectorJ/mysql-connector-java-3.1.14-bin.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at gnu.java.lang.MainThread.run(libgcj.so.7rh)
So far for the use example snippets...oh well...

So, I'm off, to the batmobile...

Thor
 
Old 04-01-2009, 05:37 AM   #7
irey
Member
 
Registered: Jun 2008
Location: Torino, Italy
Posts: 65

Rep: Reputation: 17
Yes, it's a classpath problem. From the exception's message I see you've correctly set the classpath for the driver's jar file, but you forgot to include the directory containing your classes - it didn't find LoadDriver.class. See:
Code:
LoadDriver.class not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:/usr/DBConns/ConnectorJ/mysql-connector-java-3.1.14-bin.jar],
parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
Quote:
I've possible forgotten to save it - somehow...
Evironment variables are not saved, they must be set each time the shell starts. Put it in one of your login files, such as /<your home>/.bash_profile, or (better) create a script to launch your program. Personally I usually prefer setting the classpath using command line options (-cp for java or --cp for gij).
 
Old 04-01-2009, 06:13 AM   #8
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,168
Blog Entries: 23

Original Poster
Rep: Reputation: 275Reputation: 275Reputation: 275
Thanks...this gives me something to think about (at work - I dont operate any heavy stuff ) so I may have to set the classpath to the folder where my stuff is located as well...

This was different on the windows servers I used to work with (other job - other life - other universe) as I can conclude from this: Unless I miss the point altogether...

Quote:
but you forgot to include the directory containing your classes
Of course - this is (drumroll) the first Linux server I (try to) configure...

As for the Classpath - I'll save that somewhere in a startup...as I suspected too.

You might have noticed I still need to learn several things, including - trusting Linux. It seems to be far more stable than that "baby stuff" I was used to... :-)

OK, so, off to work for me!

Tnx!

Thor
 
  


Reply

Tags
classpath, environment, java, settings


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javax classpath question kofibull Linux - Newbie 9 09-08-2006 11:03 AM
quick question k1ll3r_x Linux - Networking 2 04-11-2005 07:24 PM
Quick question... b00sted Slackware 1 12-27-2004 11:20 PM
Question Concerning ISO's and one quick question. evrae Linux - Software 2 06-21-2004 03:53 AM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM


All times are GMT -5. The time now is 08:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration