Status
Not open for further replies.

Sykora

I see right through you.
I decided to try out the Java tutorials at the Sun Microsystems Website, But I ran into some problems. See if you can help me out.

I've already installed the latest JDK, 6b2.

I followed the instructions here : *java.sun.com/docs/books/tutorial/getStarted/cupojava/unix.html , and copied the following code and saved it as HelloWorldApp.java

Code:
/**
 * The HelloWorldApp class implements an application that
 * simply prints "Hello World!" to standard output.
 */
class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

It compiles fine, but when I try to run it, I get this mess :

Code:
sykora@nexus projects/> javac HelloWorldApp.java
sykora@nexus projects/> java HelloWorldApp 
Exception in thread "main" java.lang.ClassFormatError: HelloWorldApp (unrecognized class file version)
   at java.lang.VMClassLoader.defineClass (libgcj.so.7)
   at java.lang.ClassLoader.defineClass (libgcj.so.7)
   at java.security.SecureClassLoader.defineClass (libgcj.so.7)
   at java.net.URLClassLoader.findClass (libgcj.so.7)
   at java.lang.ClassLoader.loadClass (libgcj.so.7)
   at java.lang.ClassLoader.loadClass (libgcj.so.7)
   at java.lang.Class.forName (libgcj.so.7)
   at gnu.java.lang.MainThread.run (libgcj.so.7)
sykora@nexus projects/>

Can anyone tell me what I'm doing wrong? Is it the code which is bad (I doubt) or have I installed the wrong binaries? Or is it something else?
 

QwertyManiac

Commander in Chief
Which distro are you using

For FC5:
*stanton-finley.net/fedora_core_5_installation_notes.html#Java

For Ubuntu:
*ubuntuguide.org/wiki/Dapper#How_to...nt_.28JRE.29_with_Plug-in_for_Mozilla_Firefox
 

mediator

Technomancer
Simple!

Ur problem is related to classpath! java wont execute even if u try like "java filename" in its directory in which file resides.

U can solve it by
1. Setting up the classpath
2. executing by giving full path . Like u have ur java file named "Hello.java" in /home/Sykora ...then it will compile preety easily if u give javac in that directory.
But to execute it u need to give command like "java /home/Sykora/Hello".

To setup the classpath i.e the path of directory containing all the java file and compiled java class files.........open up ur ".bash_profile" file hidden in ur home fodler and add entry at the end
***********************************
CLASSPATH=$HOME/Sykora
export CLASSPATH
***********************************


Neways u shud try 1st method..!!

UR problem Solved!!
 
Last edited:
OP
Sykora

Sykora

I see right through you.
Code:
sykora@nexus ~/> java /home/sykora/HelloWorldApp
Exception in thread "main" java.lang.NoClassDefFoundError: /home/sykora/HelloWorldApp
   at gnu.java.lang.MainThread.run (libgcj.so.7)
Caused by: java.lang.ClassNotFoundException: /home/sykora/HelloWorldApp
   at java.lang.Class.forName (libgcj.so.7)
   at gnu.java.lang.MainThread.run (libgcj.so.7)
I tried both methods. It still gives me garbage. :( Like you said, the compiling works well enough. I can't run the bytecode. Is there possible anything else I have to install? So far I've installed only jdk.
 
OP
Sykora

Sykora

I see right through you.
Which sun pack? If you mean downloading the jdk rpm from sun's website and installing it, yes I've done that. I tried doing what it said on the Stanton Finley notes but frankly, it doesn't work. I screwed up my $path and had to reboot.
 

QwertyManiac

Commander in Chief
Thats weird, it went well with me.. ah well, I dont think I can be of much help anymore now, am sorry for that.
 

JGuru

Wise Old Owl
@Sykora, you have two options to choose:

Option - I:

Uninstall the GNU Java libraries (GCJ) & install Sun's JDK 5.0.

Option -II:
Since GCJ Java libraries are installed by default, if you install Sun's JDK 5.0, you
must configure your Java settings to use Sun's JDK, not GCJ libraries.

Now the Sun's JDK will be added to your PATH. And things will work fine.
I had the same problem in Ubuntu 6.06 (Dapper Drake), & I uninstalled the
package 'gij-4.1' & installed Sun's JDK 5.0. The problem was fixed!!

In you case, the GCJ Java libraries are executing the Java bytecode ; see the
error message: 'gnu.java.lang.MainThread.run(libgcj.so.7'.
Better option is uinstalling the GCJ Java libraries. Then your problem will be solved.
Which Linux distro you are using?
 

mediator

Technomancer
@Sykora........No man I also tried to compile first and then run it from different directory then the one with CLASSPATH. It failed like u said giving the same error.
You do one thing set the classpath from terminal itself first temprorarily like
"export CLASSPATH=/home/Sykora/java_file_classfiles_directory" and then do
"java HelloWorldApp"

I am sure this solves ur problem. I also have FC5!
 

JGuru

Wise Old Owl
@Mediator, your procedure is totally wrong!!

@Sykora, Here is the solution. I hope you have installed the Java JDK 5.0 'RPM' format.
Stanton Finley's procedure is for installing JRE, so that you can view Applets. For
Java developers , this is the procedure:

Download JDK 5.0(RPM format) & also J2SE 5.0 Documentation from here
Execute the file after logging in as root.
Configuration:
Open the Terminal Window & type:
$ su -
(Enter password)

# /usr/sbin/alternatives --config java

The Output:
There are 2 programs which provide 'java'.

Selection Command
--------------------------------------------------------------------------------
* 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
+ 2 /usr/java/jdk1.5.0/bin/java

Now type 2, since you want the Sun's JDK 'java'.

# 2

Now type:
# /usr/sbin/alternatives --display java
You should see:
java - status is manual
link currently points to /usr/java/jdk1.5.0/bin/java
(The rest of the output follows here)

Also update the file : '/etc/profile.d/java.sh'
# gedit /etc/profile.d/java.sh

In the file, you should have the env variable 'JAVA_HOME'
JAVA_HOME=/usr/java/jdk1.5.0
export JAVA_HOME
PATH=$JAVA_HOME/bin: (The rest of path commands follow here)
export PATH

NOTE: Just check were Java is installed, this is given as a general procedure.
If you have installed 'JDK 5.0 Update 6' , it may be '/usr/java/jdk1.5.0_6'!!
So check it first.

Now save the file.
Logout & login to see the changes.
Now your JAVA problem should be fixed.
If you want to work in good Java IDE , as a beginner in Java language, NetBeans
is not for you. It's for Professional Java developers. Sun has developed a smaller
version of NetBeans called NetBeans IDE 5.0 BlueJ Edition. It's ideal for
programmers new to Java language & it's easy to use. Get it from here
 
Last edited:

mediator

Technomancer
@JGURU....why am I wrong? I have already reinstalled java on FC5. With this u get java and javac in ur PATH. Isnt that correct? And if u dont u can specify java PATH !! Simple!
And as for CLASSPATH ....I'm dead sure !! It will work 100%....I can bet on it!
If @Sykora has compiled it with javac then java will also work 100%.

I dunno why u say totally wrong! Please Explain!
 
Last edited:
OP
Sykora

Sykora

I see right through you.
@JGuru :
I think I have isolated my problem, or so I think.
Code:
[root@nexus]/home/sykora/downloads# rpm -ivh jdk-1_5_0_07-linux-i586.rpm
Preparing...                ########################################### [100%]
   1:jdk                    ########################################### [100%]
[root@nexus]/home/sykora/downloads# /usr/sbin/alternatives --config java

There is 1 program that provides 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java

Enter to keep the current selection[+], or type selection number:

It isn't even turning up in the menu. What have I done wrong?
 

JGuru

Wise Old Owl
@Sykora, that means that Sun's JDK was not installed!!
You need to download Sun's 'J2SE(TM) Development Kit Update 8' & click on 'Linux
RPM in self-extracting file' The file size is 45.48 MB.
Open the Terminal Window & login as root:
$ su -
(Enter password)
# ./jdk-1_5_0_08-linux-i586-rpm.bin
Now you'll get the License Agreement , Press the 'Spacebar' to move to the
next page. You must type 'yes'. Also because of Fedora's 'SE Linux' policy, which
it enforces by default. Maybe Sun's JDK was not installed!!
.So change your 'SE Linux'
policy in Fedora & try again. Java 'jdk1.5.0_08' should appear in the menu.

@Mediator, this is correct recommended way to do it.
 
Last edited:
OP
Sykora

Sykora

I see right through you.
I disabled my SELinux and redownloaded the rpm-bin file. I ran it and accepted the agreement, and installed. I'm still getting only one option on the config list. Should I try removing gcj? I'm running all of this as root, if it makes any difference.
 

mediator

Technomancer
@Jguru.....The steps u told .........i already know them! But bro....this is the correct way of just installing.
@Sykora is having problems in running the compiled file. He already has compiled the file....that means Java is running perfectly OK! and CLASSPATH is not setup coz the compiled file not being run!..............@Sykora did u setup the CLASSPATH?
 

JGuru

Wise Old Owl
@Sykora, JDK 1.5.0 RPM usually installs Java JDK in the path '/usr/java/jdk1.5.0'.
Browse your File System & go to location '/usr'.See if there is a 'java' directory under
the '/usr' directory. You can also search for 'java' using 'File Search' , in the search
directory , select 'File System'. If you do find 'jdk1.5.0_08' directory & all sub-directories
like demo etc., under it. Uinstall (remove) gcj Java libraries.
Open the file '/etc/profile.d/java.sh'
And add the line :
JAVA_HOME=/usr/java/jdk1.5.0_08
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
save the file now.
Logout & login for the changes to take effect.
 

mediator

Technomancer
JGuru said:
@Sykora, JDK 1.5.0 RPM usually installs Java JDK in the path '/usr/java/jdk1.5.0'.
Browse your File System & go to location '/usr'.See if there is a 'java' directory under
the '/usr' directory. You can also search for 'java' using 'File Search' , in the search
directory , select 'File System'. If you do find 'jdk1.5.0_08' directory & all sub-directories
like demo etc., under it. Uinstall (remove) gcj Java libraries.
Open the file '/etc/profile.d/java.sh'
And add the line :
JAVA_HOME=/usr/java/jdk1.5.0_08
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
save the file now.
Logout & login for the changes to take effect.

Heyy thats cheating! @JGURU u aint tell me why i was wrong! The stuff u posted above is for setting path which @Sykora didnt need to coz he already has "javac and java in its path".........@SYKORA....isnt it? Neways put it in ur path if u dont have!

Neways What happened to CLASSPATH? Thats guaranteed to work! Satisfaction will be guaranteed!
 
Status
Not open for further replies.
Top Bottom