Fedora Linux Support Community & Resources Center
  #1  
Old 7th October 2012, 05:31 AM
COKEDUDE Offline
Registered User
 
Join Date: Jan 2011
Posts: 351
linuxfirefox
java compile and run program help

What is the trick to get a java program to run and compile? I can't even get a basic skeleton to run and compile. What packages do I need? I figured the java-gcj and gcc-java packages was enough. Do I need any other packages?

I thought I had to do this and it would work but its not.

Code:
javac square.java 
java square
Reply With Quote
  #2  
Old 7th October 2012, 05:52 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,118
linuxfirefox
Re: java compile and run program help

The two example commands you showed were right but
you need to install the Java Development Kit:

yum install java-1.6.0-openjdk java-1.6.0-openjdk-devel

the 2nd one has the javac compiler in it so you need the java-1.6.0-openjdk-devel to actually compile java
but the java-1.6.0-openjdk is just for being able to run java code.

another option is to get the Oracle JDK and use that:

http://www.oracle.com/technetwork/ja...ads/index.html

Last edited by marko; 10th October 2012 at 12:56 AM.
Reply With Quote
  #3  
Old 7th October 2012, 07:03 AM
COKEDUDE Offline
Registered User
 
Join Date: Jan 2011
Posts: 351
linuxfirefox
Re: java compile and run program help

Quote:
Originally Posted by marko View Post
The two example commands you showed were right but
you need to install the Java Development Kit:

java-1.7.0-openjdk-devel

which has the javac compiler in it -- I think it will also pull in java-1.7.0-openjdk as a dependency

another option is to get the Oracle JDK and use that:

http://www.oracle.com/technetwork/ja...ads/index.html
Its saying I have all the packages.

Code:
$ sudo yum install java-1.6.0-openjdk*
Loaded plugins: langpacks, presto, refresh-packagekit, versionlock
Setting up Install Process
Package 1:java-1.6.0-openjdk-devel-1.6.0.0-65.1.10.8.fc15.i686 already installed and latest version
Package 1:java-1.6.0-openjdk-1.6.0.0-65.1.10.8.fc15.i686 already installed and latest version
Package 1:java-1.6.0-openjdk-src-1.6.0.0-65.1.10.8.fc15.i686 already installed and latest version
Package 1:java-1.6.0-openjdk-demo-1.6.0.0-65.1.10.8.fc15.i686 already installed and latest version
Package 1:java-1.6.0-openjdk-javadoc-1.6.0.0-65.1.10.8.fc15.i686 already installed and latest version
Nothing to do
Reply With Quote
  #4  
Old 7th October 2012, 08:25 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,118
linuxfirefox
Re: java compile and run program help

You then likely need to configure the system to use it. You use the "alternatives" command as shown here:

(for fedora 15)
http://www.mjmwired.net/resources/mjm-fedora-f15.html#java
(note: the instructions there are not totally correct for what you want, the mjmwired instructions are for the JRE, that is he's telling
you how to be able to just run Java apps, the JRE doesn't allow you to develop and compile java code. Also those instructions are
assuming you're installing the Oracle java which tends to be placed in /usr/java, the openjdk usually goes in /usr/lib/jvm )

The javac and the other commands in the OpenJDK would go in your

/usr/lib/jvm/java-1.6.0-openjdk.i686/bin

but that's not usually in your path until you run alternatives which adds a bunch of links in /usr/bin that are used to find
the commands in the jvm

Last edited by marko; 10th October 2012 at 12:56 AM. Reason: OP is on fedora 15 and not 17 and fixed arch for OP using i386
Reply With Quote
  #5  
Old 7th October 2012, 09:04 PM
COKEDUDE Offline
Registered User
 
Join Date: Jan 2011
Posts: 351
linuxfirefox
Re: java compile and run program help

Quote:
Originally Posted by marko View Post
You then likely need to configure the system to use it. You use the "alternatives" command as shown here:

http://www.mjmwired.net/resources/mj...-f17.html#java
(note: the instructions there are not totally correct for what you want, the mjmwired instructions are for the JRE, that is he's telling
you how to be able to just run Java apps, the JRE doesn't allow you to develop and compile java code. Also those instructions are
assuming you're installing the Oracle java which tends to be placed in /usr/java, the openjdk usually goes in /usr/lib/jvm )

The javac and the other commands in the OpenJDK are in

/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.6.x86_64/bin

but that's not usually in your path until you run alternatives which adds a bunch of links in /usr/bin that are used to find
the commands in the jvm
Is this right?

Code:
$ sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000
[sudo] password for bob: 
[bob@hi ~]$ 
[bob@hi ~]$ 
[bob@hi ~]$ ls -l /usr/lib/jvm
total 8
lrwxrwxrwx  1 root root   26 Jun 29 07:48 java -> /etc/alternatives/java_sdk                                                            
drwxr-xr-x  3 root root 4096 Oct  6 22:18 java-1.5.0-gcj-1.5.0.0
lrwxrwxrwx  1 root root   32 Jun 29 07:48 java-1.6.0 -> /etc/alternatives/java_sdk_1.6.0                                                
lrwxrwxrwx  1 root root   26 Jun 29 07:48 java-1.6.0-openjdk -> java-1.6.0-openjdk-1.6.0.0                                              
drwxr-xr-x. 9 root root 4096 Jun 29 07:47 java-1.6.0-openjdk-1.6.0.0
lrwxrwxrwx  1 root root   34 Jun 29 07:48 java-openjdk -> /etc/alternatives/java_sdk_openjdk                                            
lrwxrwxrwx. 1 root root   21 Sep 26  2011 jre -> /etc/alternatives/jre                                                                  
lrwxrwxrwx  1 root root   27 Oct  6 22:18 jre-1.5.0 -> /etc/alternatives/jre_1.5.0                                                      
lrwxrwxrwx  1 root root   26 Oct  6 22:18 jre-1.5.0-gcj -> java-1.5.0-gcj-1.5.0.0/jre                                                   
lrwxrwxrwx  1 root root   27 Jun 29 07:47 jre-1.6.0 -> /etc/alternatives/jre_1.6.0                                                      
lrwxrwxrwx  1 root root   30 Jun 29 07:47 jre-1.6.0-openjdk -> java-1.6.0-openjdk-1.6.0.0/jre
lrwxrwxrwx  1 root root   25 Oct  6 22:18 jre-gcj -> /etc/alternatives/jre_gcj                                                          
lrwxrwxrwx  1 root root   29 Jun 29 07:47 jre-openjdk -> /etc/alternatives/jre_openjdk                                                  
[bob@hi ~]$ ls -l /usr/java
total 4
lrwxrwxrwx 1 root root   16 Apr  4  2012 default -> /usr/java/latest
drwxr-xr-x 7 root root 4096 Apr  4  2012 jre1.6.0_27
lrwxrwxrwx 1 root root   21 Apr  4  2012 latest -> /usr/java/jre1.6.0_27                                                                
[bob@hi ~]$
Its not working. All I'm trying to run is a basic skeleton.

Code:
$ javac square.java 
[bob@hi practice1]$ java square
Exception in thread "main" java.lang.NoClassDefFoundError: square
Caused by: java.lang.ClassNotFoundException: square
        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:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: square. Program will exit.
Reply With Quote
  #6  
Old 7th October 2012, 11:35 PM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,118
linuxfirefox
Re: java compile and run program help

Quote:
Is this right?

$ sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/java/default/bin/java 20000
No, that's what I was talking about in post #4, the mjmwired instructions, for the shown alternatives --install command example, was specifically written for someone to install the Oracle JDK. Those instructions are a bit confusing for new Java users because the top of the page shows installing the rpms for openjdk.

In your case, you installed the java-1.6.0-openjdk-devel rpm (is that right? Your above --install command would be right if you did the Oracle JDK) so you want (correction in bold):
Code:
sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/lib/jvm/java-1.6.0-openjdk.i686/bin/java 20000
After you do that, it's a good idea to test that you see the proper java command via "java -version"

Last edited by marko; 10th October 2012 at 12:57 AM. Reason: OP is on i386, my original java -version was wrong for F15
Reply With Quote
  #7  
Old 9th October 2012, 12:08 PM
COKEDUDE Offline
Registered User
 
Join Date: Jan 2011
Posts: 351
linuxchrome
Re: java compile and run program help

Quote:
Originally Posted by marko View Post
No, that's what I was talking about in post #4, the mjmwired instructions, for the shown alternatives --install command example, was specifically written for someone to install the Oracle JDK. Those instructions are a bit confusing for new Java users because the top of the page shows installing the rpms for openjdk.

In your case, you installed the java-1.7.0-openjdk-devel rpm (is that right? Your above --install command would be right if you did the Oracle JDK) so you want (correction in bold):
Code:
sudo /usr/sbin/alternatives --install /usr/bin/java java /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/java 20000
After you do that, it's a good idea to test that you see the proper java command via "java -version"
which should show a result like this for the OpenJDK:

For the Oracle JDK for comparison, you'd see this for the -version:
Do I want the first one or second one? This is what I am getting.


Code:
[bob@hi practice1]$  java -version
java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.8) (fedora-65.1.10.8.fc15-i386)
OpenJDK Server VM (build 20.0-b11, mixed mode)
I am still getting the same errors .


Code:
bob@hi practice1]$ javac square.java
[bob@hi practice1]$ 
[bob@hi practice1]$ 
[bob@hi practice1]$ java square
Exception in thread "main" java.lang.NoClassDefFoundError: square
Caused by: java.lang.ClassNotFoundException: square
        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:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: square. Program will exit.
Reply With Quote
  #8  
Old 9th October 2012, 01:09 PM
markkuk Offline
Registered User
 
Join Date: Apr 2005
Location: Finland
Posts: 5,075
windows_7firefox
Re: java compile and run program help

Maybe the problem is with your code, not the compiler. Can you show the contents of "square.java"? Is there a "square.class" file in the directory after running javac?
Reply With Quote
  #9  
Old 10th October 2012, 12:18 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,118
linuxfirefox
Re: java compile and run program help

Another big help is to install "galternatives" which is a gui tool that can display the settings in the alternatives system in Fedora easily without having to type those hard to remember command line
alternative commands. You can also install and configure your java settings with it
Quote:
su -
<root password>
yum install -y galternatives
galternatives
then compare the settings for "java" and "javac", I suspect the problem might be that java is pointing to your new JDK but javac is set to nothing or an old entry.

Why they don't make it simple and have java point to the root of the JDK installation so that all the different tools like java, javaws, javac are always consistent (ie. the ones in <java root>/bin directory)
I don't know.

Basically if you're doing lots of compiling and running java on the command line, you need to make sure that all the commands use the same consistent JDK,
for example these version outputs should show as from the same JDK:
Quote:
java -version

javac -version
If you use a real IDE like Netbeans or Intelli J, the IDE makes sure this stuff is sensible ( you usually tell it where the JDKs are in the IDE's configuration, you can have several) and even if the
alternatives configuration is wrong in the OS in that case, it will still work and compile in the IDE. In fact I just found out my system's alternatives settings were broken and I didn't know
because I use IDE's (netbeans 7.2 and intellij 11.2 mostly). I had java pointing to 1.7.0.7 and javac set to 1.7.0.6. Actually java is pretty good at working with that, it would have been compiling with update 6 and running on update 7 which would likely be fine.

PS. I've just noticed you're running on the old Fedora 15, I'm on Fedora 17 so your JDK from the repositories is a lot older so some of my quoted versions won't match yours, so the correct mjmwired URL would be: http://www.mjmwired.net/resources/mjm-fedora-f15.html

Last edited by marko; 10th October 2012 at 12:58 AM.
Reply With Quote
  #10  
Old 10th October 2012, 01:25 AM
LongTimeDabbler Offline
Registered User
 
Join Date: Jan 2011
Posts: 143
linuxchrome
Re: java compile and run program help

I don't know but you might need to set JAVA_HOME env var.
Reply With Quote
  #11  
Old 10th October 2012, 05:47 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,768
linuxfedorafirefox
Re: java compile and run program help

Quote:
Originally Posted by COKEDUDE View Post
I am still getting the same errors .
Code:
bob@hi practice1]$ javac square.java
[bob@hi practice1]$ java square
Exception in thread "main" java.lang.NoClassDefFoundError: square
Caused by: java.lang.ClassNotFoundException: square
        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:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: square. Program will exit.
Do this (once) before running the java command:
Code:
export CLASSPATH=.
That will let the java command know that the current directory (.) can contain Java class files. Put that export command in your ~/.bashrc to make it permanent.
Reply With Quote
  #12  
Old 10th October 2012, 05:57 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,118
linuxfirefox
Re: java compile and run program help

But doesn't the current "." directory automatically exist as a class search directory? I can build a class (example, Classname) in the current directory and run
them with:
Quote:
java Classname
just fine without having CLASSPATH=. set or passing "-cp ." on the command line . I was thinking he had his class in the current directory

Last edited by marko; 10th October 2012 at 06:00 AM.
Reply With Quote
  #13  
Old 11th October 2012, 12:30 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,768
linuxfedorafirefox
Re: java compile and run program help

Quote:
Originally Posted by marko View Post
But doesn't the current "." directory automatically exist as a class search directory?
No, not if you have only the Oracle JDK installed (from the .bin file, not the .rpm version). It's possible that the openjdk packages for Fedora have some initialization or wrapper script that puts . in the CLASSPATH, but I never install openjdk so I can't say for sure.
__________________
OS: Fedora 21 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #14  
Old 11th October 2012, 02:00 AM
marko Offline
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 6,118
linuxfirefox
Re: java compile and run program help

I usually use the oracle JDK 1.7.0u7 and it works just fine at finding the class in the current directory without the CLASSPATH=. in the environment nor "-cp . " or "-classpath . "

I proved that by compiling a class and running it on the command line without any of those options:

Quote:
echo $CLASSPATH
CLASSPATH: Undefined variable.
Quote:
java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
Quote:
javac SomeClass.java
java SomeClass
something
where my SomeClass.java is just containing a trivial test class that just says "something"

Code:
public class SomeClass {

             public static void main(String[] args) {
                         System.out.println("something");
             }
}
I think you might be meaning more complex cases where you have the package set and have the classes in directories

-----

Last edited by marko; 11th October 2012 at 01:58 AM.
Reply With Quote
  #15  
Old 11th October 2012, 03:00 AM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,768
linuxfedorafirefox
Re: java compile and run program help

Quote:
Originally Posted by marko View Post
I think you might be meaning more complex cases where you have the package set and have the classes in directories
No, just plain, single class files. Actually I think it's caused not by the Oracle JDK but by several applications that I run which set the CLASSPATH variable. So on my machine without explicitly doing CLASSPATH=. (which adds . to the end of any existing CLASSPATH) Java won't look for class files in the current directory. Maybe COKEDUDE also has something setting CLASSPATH, in which case including . would be necessary.
__________________
OS: Fedora 21 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
Reply

Tags
compile, java, program, run

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to compile the c program simhakln123 Using Fedora 1 9th December 2010 12:36 PM
not able to compile the c program simhakln123 Using Fedora 1 9th December 2010 08:31 AM
Compile 32-bit Program on a 64-bit OS cc7gir Using Fedora 4 20th January 2010 01:13 AM
how to use shell program parameters in java program?? srujanabobba Using Fedora 1 10th August 2006 08:24 PM
How to compile java program in eclipse? siegfried Using Fedora 3 19th November 2005 12:23 AM


Current GMT-time: 20:18 (Thursday, 18-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Creek Side - Stockholm-Arlanda Airport (ARN) Travel Photos on Instagram - Marina of Koper Travel Photos - Te Whiti Park Instagram Photos - Texas State Fair Photos on Instagram