ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.)
- 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
- 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:
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?
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:
// 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
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...
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)
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).
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... :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.