LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-24-2009, 09:38 AM   #1
kft4v
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Rep: Reputation: 0
Problem with jdk class files


I'm quite new at linux and Java and I am encountering a problem..
I started learning java and i downloaded a jdk program but not the one sun provides... I compiled and ran my first program, HelloWorld, which as you imagine displayed a hello world line -.-
Now i tried to compile my second program, the source of which is this:
Code:
import java.util.Scanner;
public class paradox {
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
int count=1;
int number=in.nextInt();
in.close();
while (number != 1) {
              count ++;
boolean even = (number % 2)==0;
if (even){
	number=number/2;
}
else{
	number=(number*3)+1;
}
System.out.println(count + ". " + number);
}
}
}
In short what this does is checking if a number is odd or even then takes an action (multiplies odd ones by 3 then adds 1 or divides even ones by 2) and repeats until number 1 is produced, which is a computing paradox. After multiple emails with a guy I finally find out that I need sun's jdk in order for this to be compiled (some lines are different for other versions, or sth like that) so i uninstall my version download the sun rpm.bin from their site install it after some googling and compile it
When i try to run my .class file either by
Code:
java -cp . paradox
or by
Code:
java -cp . paradox.class
i always get the following error:

kft4v@kft4v-laptop:~/Java$ java -cp . paradox
java.lang.ClassNotFoundException: paradox not found in java.lang.ClassLoader$1{urls=[file:/home/kft4v/Java/./], parent=null}
at java.net.URLClassLoader.findClass (URLClassLoader.java:870)
at java.lang.ClassLoader.loadClass (ClassLoader.java:359)
at java.lang.ClassLoader$1.loadClass (ClassLoader.java:1333)
at java.lang.ClassLoader.loadClass (ClassLoader.java:310)
at java.lang.VirtualMachine.main (VirtualMachine.java:99)

Anyone has any idea what i should do? Thanks

Last edited by kft4v; 01-25-2009 at 10:06 AM.
 
Old 01-24-2009, 12:12 PM   #2
AGer
Member
 
Registered: Oct 2007
Distribution: Slackware current
Posts: 136
Blog Entries: 22

Rep: Reputation: 19
Since you have paradox.class, you already did it, but to be complete

$javac paradox.java

Next in order to run it

$java paradox

You do not need any options to run such a class and you do not need the .class extension. Your code works:

bash-3.1$ java paradox
34
2. 17
3. 52
4. 26
5. 13
6. 40
7. 20
8. 10
9. 5
10. 16
11. 8
12. 4
13. 2
14. 1
bash-3.1$

If you want counting that starts from 1, drop the "count++" and use ++ in "System.out.println(count++ + ". " + number);"
 
Old 01-24-2009, 04:29 PM   #3
kft4v
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Original Poster
Rep: Reputation: 0
ye i know my code works, and it gets compiled just fine, for some crazy reason the class file can't run. Should i set anything other than JAVA_PATH? because when i try to run it (not compile it) it tells me this:

kft4v@kft4v-laptop:~/Java$ java paradox
java.lang.ClassNotFoundException: paradox not found in java.lang.ClassLoader$1{urls=[file:/home/kft4v/Java/./], parent=null}
at java.net.URLClassLoader.findClass (URLClassLoader.java:870)
at java.lang.ClassLoader.loadClass (ClassLoader.java:359)
at java.lang.ClassLoader$1.loadClass (ClassLoader.java:1333)
at java.lang.ClassLoader.loadClass (ClassLoader.java:310)
at java.lang.VirtualMachine.main (VirtualMachine.java:99)
 
Old 01-25-2009, 06:58 AM   #4
AGer
Member
 
Registered: Oct 2007
Distribution: Slackware current
Posts: 136
Blog Entries: 22

Rep: Reputation: 19
It may help if you add your distribution to your profile so that somebody with exactly the same distribution may have a look.

It may also e useful to know what exactly did you download from Sun, or at least the output of java -version and javac -version.

No, all that is to be done to setup Java is

Code:
export JAVA_HOME=/usr/lib/java
export MANPATH="${MANPATH}:${JAVA_HOME}/man"
export PATH="${PATH}:${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin"
as it is done by the Slackware jdk package.

I would do some experiments:

Can you compile and run your first "Hello World" program, that is, does the new Java from the rpm work with it?

What is the output for a known problem, like "java parad"? It is interesting, since my Sun jdk 6u11 java uses apparently different wording:

Code:
bash-3.1$ java parad
Exception in thread "main" java.lang.NoClassDefFoundError: parad
Caused by: java.lang.ClassNotFoundException: parad
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: parad.  Program will exit.
bash-3.1$
 
Old 01-25-2009, 10:14 AM   #5
kft4v
LQ Newbie
 
Registered: Jan 2009
Posts: 4

Original Poster
Rep: Reputation: 0
I can compile both Helloworld.java and paradox.java just fine. The same (or a similar) error is reported when I try to run either of the .class files that the javac command produces. I have the latest ubuntu release, I think, 8.04 or something, not sure though
 
Old 01-26-2009, 04:47 AM   #6
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,499

Rep: Reputation: 91
Hmm I was once stumped by the same type of error for days.

What eventually turned out to be the problem was that I was running the WRONG "java" binary!

Yep, I know it sounds stupid / non-sensical, but I had two "java" binaries - one was the -real- SUN Microsystems java binary (it was in /usr/local/bin if I remember right) and the -other- was a "java" binary (I can't even remember where I got it - I think it was some package called Kaffe or something, or was installed by my distro automatically - Fedora Core 6) which was in /usr/bin...

I'm sure you see what I'm getting at - in my PATH, /usr/bin was listed BEFORE /usr/local/bin. This caused that, each time I called "java" in a shell window, the "incorrect" java was called. Same as you, I could compile everything fine (my system has only ONE javac binary...) but NOTHING wanted to work when I tried to run the compiled .jar

Took me hours to find this out. Eventually I solved this by searching my entire harddisk for ANY file called "java" - like this, when logged in as root:

Code:
find / -name java
That was where I spotted that I had TWO java binaries, in two seperate bin directories.

What I'm getting at is check your system that you have only ONE "java" binary - if you, like me, maybe have two... especially since, after the fact, you downloaded and installed your "own" instance of the Java SDK. If you DO have two, run the correct one by going

Code:
/usr/local/bin/java application.jar
or whatever is the ACTUAL SUN Java SDK JVM binary for your system.

This is how I solved my issue with compiling fine, but absolutely refusing to run. Turns out the "other java" I had was completely deperecated and incompatible with what I was trying to do...

Last edited by rylan76; 01-26-2009 at 04:49 AM.
 
Old 01-26-2009, 05:58 AM   #7
AGer
Member
 
Registered: Oct 2007
Distribution: Slackware current
Posts: 136
Blog Entries: 22

Rep: Reputation: 19
The rylan76 case does look similar.

This is why I was asking for the output for a known problem - just to see if it looks like the genuine Sun Java.
Some other tests may be helpful:

Code:
$which java
$which javac
$java -version
$javac -version
It would also avoid wording like "the same (or a similar)" since it does not help much. When asking a question like that the main hope is with tiny details that may help to guess what is wrong.

So far, the only new fact we got to know is that the Hello World stopped running when Sun Java has been installed. Thus, most likely, the Java is not installed properly. I see the following options:

1. Collect additional data as explained above in hope to figure out what is wrong.
2. Locate all kinds of Java, trash that, and reinstall Sun JDK in hope that the install will be clean and successful.
3. Reinstall Linux so that you know what exactly you are using.
 
  


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
What's the Difference between Header files and a Class? Ryupower Programming 14 07-05-2012 09:37 AM
java class files trscookie Programming 9 05-13-2006 05:06 PM
where to put bean class files ? ihasircioglu Programming 0 12-11-2003 04:20 PM
Running Java Class Files hackersapien Programming 9 08-07-2003 06:42 AM
can't run java class files in linux JIV Programming 6 03-25-2003 01:20 AM


All times are GMT -5. The time now is 12:33 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration