You are not logged in.

#1 2010-04-03 20:14:21

rob1101
Member
Registered: 2008-02-23
Posts: 28

java classpath

I am trying to compile and run a simple java program, i have openjdk6 installed.


~/.bashrc

# Check for an interactive session
[ -z "$PS1" ] && return

PS1='\[\e[0;37m\][\u@\h \W]\$ \[\e[0m\]'

JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export JAVA_HOME

export PATH=$PATH:/usr/lib/jvm/java-6-openjdk/bin

CLASSPATH=/usr/lib/jvm/java-6-openjdk/lib/tools.jar
export CLASSPATH

test.java

import java.io.*;

public class test{
    public static void main(){
        System.out.print("Testing");
    }
}

"$ javac test.java" compiles fine but it trhows an error when i try to run it with "$ java test.java"

Exception in thread "main" java.lang.NoClassDefFoundError: test/java
Caused by: java.lang.ClassNotFoundException: test.java
    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:319)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: test.java. Program will exit.

Offline

#2 2010-04-03 23:29:56

Pajaro
Member
Registered: 2004-04-21
Posts: 884

Re: java classpath

try adding working directory (.) to the classpath, specifying the main class or running it without specifying the .java extension.

Offline

#3 2010-09-03 19:06:05

soderstrom
Member
Registered: 2010-08-30
Posts: 10

Re: java classpath

I don't understand what Pajaro try to say, but I get the same error as above and don't know how to fix it. I read and followed the wiki how to install sun jdk and jre.

Javac works, but to run the compiled file gives me error and I can't even copy and past the errors here...is it something wrong with urxvt?

Work fine to compile and execute the program in geany with xterm. So seems the problem is with urxvt.

Offline

#4 2010-09-04 04:17:36

infiernando
Member
Registered: 2010-09-04
Posts: 1

Re: java classpath

Try "java test" only, without the ".java" part

Offline

#5 2010-09-04 05:28:16

tas
Member
Registered: 2010-09-03
Posts: 2

Re: java classpath

try:

javac test.java

T compile the source code into .class file.

and then:

java test

To run it.

Offline

#6 2010-09-04 07:22:59

eDio
Member
From: Ukraine, Kyiv
Registered: 2008-12-02
Posts: 422

Re: java classpath

2soderstrom
As for Pajaro's answer:
. in classpath will allow you to run java classes from current working directory. To be precise, if you have

package foo
public class Bar {}

in your ~/test/, then you'll be able to run class from test directory with
$ java foo.Bar

Offline

#7 2010-09-04 07:30:14

soderstrom
Member
Registered: 2010-08-30
Posts: 10

Re: java classpath

Thanks, it works now smile

Offline

#8 2010-09-04 09:08:57

tobier
Member
From: Stockholm, Sweden
Registered: 2010-07-05
Posts: 11
Website

Re: java classpath

rob1101 wrote:

I am trying to compile and run a simple java program, i have openjdk6 installed.


~/.bashrc

# Check for an interactive session
[ -z "$PS1" ] && return

PS1='\[\e[0;37m\][\u@\h \W]\$ \[\e[0m\]'

JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export JAVA_HOME

export PATH=$PATH:/usr/lib/jvm/java-6-openjdk/bin

CLASSPATH=/usr/lib/jvm/java-6-openjdk/lib/tools.jar
export CLASSPATH

test.java

import java.io.*;

public class test{
    public static void main(){
        System.out.print("Testing");
    }
}

"$ javac test.java" compiles fine but it trhows an error when i try to run it with "$ java test.java"

Exception in thread "main" java.lang.NoClassDefFoundError: test/java
Caused by: java.lang.ClassNotFoundException: test.java
    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:319)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: test.java. Program will exit.

You're trying to run the file test.java, not the class. I think $java test.java would mean something that you would like to run the class "java" in the package "test".
Compile and run with

$ javac test.java
$ java test

Offline

Board footer

Powered by FluxBB