LinuxQuestions.org
Review your favorite Linux distribution.
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 11-17-2011, 01:50 PM   #1
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Rep: Reputation: Disabled
set java classpath on linux


Hello!
I'm a new user in Linux. I'm using opensuse 11.2, but i don't know how to set path to java. The problem in console:
Code:
Exception in thread "main" java.lang.NoClassDefFoundError: myfirstprogram
Caused by: java.lang.ClassNotFoundException: myfirstprogram
	at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: myfirstprogram. Program will exit.
Is this echo $PATH? Where it have to be wrote?
 
Old 11-17-2011, 02:58 PM   #2
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,227
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
Quote:
Is this echo $PATH? Where it have to be wrote?
No, it's not. Test it, enter this in the console:

Quote:
java -version
...there should be some info comming back.

The line to worry about is:
Quote:
java.lang.NoClassDefFoundError
...you're using something that java cannot find. The path has to be set to that component as well...

Happy coding!

Thor
 
Old 11-17-2011, 03:25 PM   #3
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Java searches its CLASSPATH for classes; nothing to do with the shell's path. From man java:
Code:
          -classpath classpath

          -cp classpath
             Specify a list of directories, JAR archives, and ZIP archives to search for class files. Class path entries are
             separated by colons (:). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.

          If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the current directory
          (.).
So you either set CLASSPATH=/your/path in the shell or you add -classpath or -cp to the java command.
 
1 members found this post helpful.
Old 11-17-2011, 05:30 PM   #4
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
How I can to find the Classpath? Give me an example.

Last edited by rotation; 11-17-2011 at 05:45 PM.
 
Old 11-18-2011, 02:37 PM   #5
Thor_2.0
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,227
Blog Entries: 23

Rep: Reputation: 279Reputation: 279Reputation: 279
Find it with:

Quote:
echo $CLASSPATH
set it (as SecretCode pointed out) like so

Quote:
CLASSPATH=/some/path;some/other/path
This can be persisted if you add this (last) line to, say, /etc/rc.conf - depending on your distro, on OpenSuse, you may need to look into the /etc/rc.d folder instead...

Thor
 
Old 11-19-2011, 03:12 AM   #6
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Who is the quick CLASSPATH?
echo $CLASSPATH is empty!
 
Old 11-20-2011, 04:02 AM   #7
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
It depends where you have installed java and more importantly where your code is.

Your error says it can't find myfirstprogram so you need to give the location of myfirstprogram.class - either in $CLASSPATH or on the java command line
 
Old 11-23-2011, 01:09 PM   #8
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
linux-kzpj:/home/denislav/Desktop # CLASSPATH=/home/denislav/Download/;/home/denislav/Desktop/myfirstjavaprog.java
bash: /home/denislav/Desktop/myfirstjavaprog.java: Permission denied


OR


linux-kzpj:/home/denislav/Desktop # java /home/denislav/Desktop/myfirstjavaprog /home/denislav/Desktop/myfirstjavaprog.class
Exception in thread "main" java.lang.NoClassDefFoundError: /home/denislav/Desktop/myfirstjavaprog
Caused by: java.lang.ClassNotFoundException: .home.denislav.Desktop.myfirstjavaprog
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: /home/denislav/Desktop/myfirstjavaprog. Program will exit.

My myfrsjavaprog is in /home/denislav/Desktop/myfirstjavaprog.java What I have to make?
 
Old 11-23-2011, 01:22 PM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by rotation View Post
linux-kzpj:/home/denislav/Desktop # CLASSPATH=/home/denislav/Download/;/home/denislav/Desktop/myfirstjavaprog.java
bash: /home/denislav/Desktop/myfirstjavaprog.java: Permission denied


OR


linux-kzpj:/home/denislav/Desktop # java /home/denislav/Desktop/myfirstjavaprog /home/denislav/Desktop/myfirstjavaprog.class
Hi,

try quoting your classpath variable:
Code:
CLASSPATH="/home/denislav/Download/;/home/denislav/"
Alternatively, you can call your program like:
Code:
java -cp /home/denislav/Desktop/ myfirstjavaprog
Notice, that your CLASSPATH contains the directory where your program resides; the name of the program is not part of the classpath.
 
Old 11-23-2011, 01:29 PM   #10
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
denislav@linux-kzpj:~/Desktop> java -cp /home/denislav/Desktop/ myfirstjavaprog
Exception in thread "main" java.lang.UnsupportedClassVersionError: myfirstjavaprog : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:637)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: myfirstjavaprog. Program will exit.

Any idea?
 
Old 11-26-2011, 06:09 AM   #11
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Lots of web search results for "UnsupportedClassVersionError". You may have an older Java version on the host where you are trying to run this.

What do you get from
Code:
java -version
 
Old 11-26-2011, 08:05 AM   #12
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
denislav@linux-kzpj:~> java -version
java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.6) (suse-5.9.4-i386)
OpenJDK Server VM (build 14.0-b16, mixed mode)
 
Old 11-26-2011, 01:11 PM   #13
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
And did you compile the myfirstjavaprog class using a later compiler version? That message is saying that somehow myfirstjavaprog.class expects a higher java runtime version than you are running it with.

Do you have a $JAVA_HOME environment variable set?
 
Old 11-26-2011, 01:25 PM   #14
rotation
LQ Newbie
 
Registered: Nov 2011
Posts: 14

Original Poster
Rep: Reputation: Disabled
Code:
denislav@linux-kzpj:~/Desktop> $JAVA_HOME
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
where options include:
    -d32	  use a 32-bit data model if available
    -d64	  use a 64-bit data model if available
    -client	  to select the "client" VM
    -server	  to select the "server" VM
    -hotspot	  is a synonym for the "client" VM  [deprecated]
                  The default VM is server,
                  because you are running on a server-class machine.


    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -D<name>=<value>
                  set a system property
    -verbose[:class|gc|jni]
                  enable verbose output
    -version      print product version and exit
    -version:<value>
                  require the specified version to run
    -showversion  print product version and continue
    -jre-restrict-search | -jre-no-restrict-search
                  include/exclude user private JREs in the version search
    -? -help      print this help message
    -X            print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:hprof
                  see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
See http://java.sun.com/javase/reference for more details.

Last edited by rotation; 11-26-2011 at 02:10 PM.
 
Old 11-26-2011, 02:01 PM   #15
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by rotation View Post
denislav@linux-kzpj:~/Desktop> $JAVA_HOME
Usage: ...
1: Always remember to use [code][/code] tags around your code

2: When you want to find out the value of an environment variable, use echo
Code:
echo $JAVA_HOME
What you've done is to execute the contents which could have been very dangerous!
 
  


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
how to set java classpath on Redhat 9? antony_csf Programming 9 11-26-2004 06:48 AM
How to set CLASSPATH of java in Linux? vanhelsing Programming 1 08-23-2004 07:16 AM
set classpath in linux(slackware9) for java Manjula.S Linux - Software 1 10-16-2003 03:10 AM
set up path,classpath in linux(slaware9) for java Manjula.S Linux - Software 5 10-07-2003 12:38 AM
How to set classpath for java jolly Linux - Software 3 04-22-2003 11:02 PM


All times are GMT -5. The time now is 07:44 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration