Oops, on the last example, set the classpath for a jar as follows: export CLASSPATH=<pathtojar>/MyHelloWorld.jar e.g. Using /opt/myapp/lib as <pathtoyourclasslocation> export CLASSPATH=/opt/myapp/lib/MyHelloWorld.jar HTH, -Bob > Working with classpaths in Java isn't too hard, but there are some rules > to follow. > > 1. Simplest scenario: Executing in the same directory, and there are no > package definitions in your source code. Example: HelloWorld, no package > definition. Execute from that directory, no classpath definition needed. > eg. > > Class HelloWorld { ... } > > 2. Next simplest: You compile without packages defined: > using the same directory for source and compiled classes, or if you > compiled classes to a different directory: > > export CLASSPATH=<pathtoyourclasslocation> > example directory: /opt/myapp/lib/ > then > export CLASSPATH=/opt/myapp/lib/ > and call it: > java HelloWorld > > > 3. Adding a package definition to your source code, and there is a > definite source code hierachy. Using the package example below: > > package project.testjava; > Class HelloWorld { ... } > > Your directory structure should look like this: > <pathtoyoursourcelocation>/project/testjava/HelloWorld.java > > Assuming that you compile your code in the same directory and deposit the > class files there, then set your classpath as follows: > > export CLASSPATH=<pathtoyoursourcelocation> > e.g. Using /opt/myapp/source/ as <pathtoyoursourcelocation>: > ex. export CLASSPATH=/opt/myapp/source/ > > and call the application as follows: > > java project.testjava.HelloWorld > > -------------------------------------------- > > If you compile to a different location, then your directory structure will > look like this after a compilation: > > <pathtoyoursourcelocation>/project/testjava/HelloWorld.java > <pathtoyourclasslocation>/project/testjava/HelloWorld.class > > Then set the classpath as follows: > export CLASSPATH=<pathtoyourclasslocation> > e.g. Using /opt/myapp/lib as <pathtoyourclasslocation> > export CLASSPATH=/opt/myapp/lib > > and call the application as follows: > > java project.testjava.HelloWorld > > 3. You've compiled to a jarfile, e.g. MyHelloWorld.jar > export CLASSPATH=<pathtojar>/MyHelloWorld.jar > e.g. Using /opt/myapp/lib as <pathtoyourclasslocation> > export CLASSPATH=/opt/myapp/lib > > and call as follows: > java project.testjava.HelloWorld > > > -Bob > >> On Saturday 29 May 2004 05:44 pm, you wrote: >>> That didn't work. I still get the NoClassDefFoundError. Any other >>> ideas? >> >> You're right !! it doesn't work. I just try this myself, and whenever I >> try to >> execute from outside the directory where the java files is located, I >> got >> the >> same error. I thought I've seen this and what I suggested was the >> solution, >> obviously not ! (Now it should be obvious that I am not a java developer >> myself, I just play with it sometime) >> >> After some searching myself, I only have this solution: >> >> If for example, my java program is in >> ~/project/testjava/helloworld.java, >> I >> can put in helloworld.java: >> >> package project.testjava; >> <rest of the src code> >> >> then I need to go to ~, and compile it from there: >> javac project/testjava/helloworld.java >> >> and running it from there would work tpp: >> java project/testjava/helloworld >> >> The problems with the solution: First, I can't find a way so that I can >> use >> absolute path during compile and running (which is probably what you >> want >> in >> your case, since you're going to put the command in rc.local). So what I >> want >> is something so that I can say: >> >> javac /home/<username>/project/testjava/helloworld.java >> java /home/<username>/project/testjava/helloworld >> >> when I tried, the solution does not work. I can't also figure out what >> to >> put >> in after the "package" keyword in the source code to make the absolute >> path >> work. >> >> Secondly, the source code is dependent on what directory you want to >> compile >> and run the java program from. Unless I am missing something, that is >> just >> plain stupid !! Source code of a program should never be dependent on >> where >> the program is running from. >> >> So that's it. I am kinda stuck right now too. I don't know if that would >> help >> you, but if you find the real solution, please let me know, cause right >> now >> it bugs the hell out of me too ! (BTW, I have a task of writing some >> java >> program in the near future, so this would be good to know). >> >> I know this is going OT, but if anyone else on the list know a solution, >> please help. >> >> RDB >> >> >>> On Sat, 2004-05-29 at 11:59, Reuben D. Budiardja wrote: >>> > On Saturday 29 May 2004 12:16 pm, Michael Sullivan wrote: >> <snip>I wrote a java server that I want to start every time >>> > > my server PC restarts. I created a /usr/local/classes directory >>> and >>> > > copied Server.class and the support classes it uses there. Id cd'd >>> to >>> > > /usr/local/classes and issued a java Server from there and it works >>> > > fine, but if I issue a java /usr/local/classes/Server from anywhere >>> > > outside the /usr/local/classes directory it tells me >>> > > >>> > > Exception in thread "main" java.lang.NoClassDefFoundError: >>> > > /usr/local/classes/Server >> <snip> >>> > Try the following: >>> > set the environment variable CLASSPATH to /usr/local/classes so that >>> Java >>> > knows where to find your classes. Asumming you're using bash: >>> > $> export CLASSPATH=$CLASSPATH:/usr/local/classes >>> > >> >> >> -- >> Reuben D. Budiardja >> Department of Physics and Astronomy >> The University of Tennessee, Knoxville, TN >> --------------------------------------------------------- >> "To be a nemesis, you have to actively try to destroy >> something, don't you? Really, I'm not out to destroy >> Microsoft. That will just be a completely unintentional >> side effect." >> - Linus Torvalds - >> >> >> -- >> redhat-list mailing list >> unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe >> https://www.redhat.com/mailman/listinfo/redhat-list >> > > > -- > redhat-list mailing list > unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe > https://www.redhat.com/mailman/listinfo/redhat-list > -- redhat-list mailing list unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe https://www.redhat.com/mailman/listinfo/redhat-list