LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   set java classpath on linux (https://www.linuxquestions.org/questions/linux-newbie-8/set-java-classpath-on-linux-914077/)

rotation 11-17-2011 12:50 PM

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?

ButterflyMelissa 11-17-2011 01:58 PM

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

SecretCode 11-17-2011 02:25 PM

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.

rotation 11-17-2011 04:30 PM

How I can to find the Classpath? Give me an example.

ButterflyMelissa 11-18-2011 01:37 PM

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

rotation 11-19-2011 02:12 AM

Who is the quick CLASSPATH?
echo $CLASSPATH is empty!

SecretCode 11-20-2011 03:02 AM

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

rotation 11-23-2011 12:09 PM

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?

crts 11-23-2011 12:22 PM

Quote:

Originally Posted by rotation (Post 4531910)
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.

rotation 11-23-2011 12:29 PM

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?

SecretCode 11-26-2011 05:09 AM

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

rotation 11-26-2011 07:05 AM

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)

SecretCode 11-26-2011 12:11 PM

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?

rotation 11-26-2011 12:25 PM

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.


SecretCode 11-26-2011 01:01 PM

Quote:

Originally Posted by rotation (Post 4534565)
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!


All times are GMT -5. The time now is 07:51 PM.