LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Classpath - quick question (http://www.linuxquestions.org/questions/programming-9/classpath-quick-question-715739/)

Thor_2.0 03-31-2009 05:57 AM

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)...

taylor_venable 03-31-2009 08:34 AM

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.)

irey 03-31-2009 08:41 AM

Quote:

Originally Posted by linusr@flanders (Post 3493508)
- 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 (Post 3493508)
- 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 (Post 3493508)
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?

Thor_2.0 03-31-2009 04:42 PM

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

irey 04-01-2009 03:08 AM

nosuchdevfoundexception??? Never heard about it before, not even google knows about it :p. Did you mean NoClassDefFoundError or ClassNotFoundException?

Thor_2.0 04-01-2009 03:51 AM

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 :D :D

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

irey 04-01-2009 05:37 AM

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).

Thor_2.0 04-01-2009 06:13 AM

Thanks...this gives me something to think about (at work - I dont operate any heavy stuff :D ) 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


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