You are not logged in.
Pages: 1
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
try adding working directory (.) to the classpath, specifying the main class or running it without specifying the .java extension.
Offline
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
Try "java test" only, without the ".java" part
Offline
try:
javac test.java
T compile the source code into .class file.
and then:
java test
To run it.
Offline
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
Thanks, it works now
Offline
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
Pages: 1