# Java Trouble



## Sykora (Aug 12, 2006)

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


```
/**
 * 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 :


```
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 (Aug 12, 2006)

I dont know where the problem lies but please try to use the standard Sun Java 5 instead of GCJ libraries.


----------



## Sykora (Aug 12, 2006)

Can you tell me how I install those?


----------



## QwertyManiac (Aug 12, 2006)

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


----------



## harmax (Aug 12, 2006)

use netbeans www.netbeans.org itll solve al ur probs


----------



## mediator (Aug 12, 2006)

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!!


----------



## Sykora (Aug 12, 2006)

```
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.


----------



## QwertyManiac (Aug 12, 2006)

Did you install the Sun Java pack yet? GCJ gives me such errors too


----------



## Sykora (Aug 12, 2006)

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 (Aug 12, 2006)

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 (Aug 12, 2006)

@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 (Aug 12, 2006)

@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 (Aug 12, 2006)

@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


----------



## mediator (Aug 13, 2006)

@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!


----------



## Sykora (Aug 13, 2006)

@JGuru :
I think I have isolated my problem, or so I think.

```
[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 (Aug 13, 2006)

@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.


----------



## Sykora (Aug 13, 2006)

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 (Aug 13, 2006)

@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 (Aug 13, 2006)

@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 (Aug 13, 2006)

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
> ...



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!


----------



## JGuru (Aug 13, 2006)

@Mediator, The Java libraries that are installed by default are GNU Java libraries.
 Sun's JDK is the reference implementation. The Java @Sykora has is gcj libraries,
 not Sun's JDK!! This is not cheating. I have more than 5 years working experience
in Java. How many years experience you have @Mediator? How many Java 
applications you have developed?? I'm Sun Cerified Java Programmer. I also hold 
BrainBench certification in Java. Let's put an end to this unnecessay futile argument!!


----------



## mediator (Aug 13, 2006)

Heyyy! @JGURU Calm down. No args. But u shud have told y I was wrong.
Ok u have more experience! I have worked for around 2 yrs on java now and have made many applications in java mainly in networking area!

But what @Sykora experience .......Exactly I also experienced that! And I have solved it by just configuring CLASSPATH!  Dont get so proud because u have more experience. Most of the times even teachers think that a student is useless and doesnt know anything.....whereas the fact can be very contrasting and opposite!

EPEACE!


----------



## mehulved (Aug 13, 2006)

This thread is so confusing. I am facing the same problem too for many java programs. I am on ubuntu 6.06 and it seems I have Sun Java recognised here.


----------



## mediator (Aug 13, 2006)

Sykora said:
			
		

> 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
> ...



U tried only the 2nd way? Please try the first method too! and @tech_for_future u too! Atleast tell the outcome of CLASSPATH method! U wont have to write "java /home/sykora/HelloWorldApp" then ..............only "java HelloWorldApp"......and that too anywhere, in any directory !


----------



## harmax (Aug 13, 2006)

guys dont bang ur head, jus use netbeans or eclipse , and put ur efforts in creating applications rather than tryin to get it to even compile

yeah i do accept that my solution is jus a dirty work around


----------



## Sykora (Aug 13, 2006)

Thanks JGuru, your solution worked flawlessly. I think I was getting mixed up in the path where my java was installed. I don't know why.


----------

