 |
 |
 |
 |
| Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum. |

7th October 2012, 04:31 AM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 275

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

7th October 2012, 04:52 AM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449

|
|
|
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; 9th October 2012 at 11:56 PM.
|

7th October 2012, 06:03 AM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 275

|
|
|
Re: java compile and run program help
Quote:
Originally Posted by marko
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
|

7th October 2012, 07:25 AM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449

|
|
|
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; 9th October 2012 at 11:56 PM.
Reason: OP is on fedora 15 and not 17 and fixed arch for OP using i386
|

7th October 2012, 08:04 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 275

|
|
|
Re: java compile and run program help
Quote:
Originally Posted by marko
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.
|

7th October 2012, 10:35 PM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449

|
|
|
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; 9th October 2012 at 11:57 PM.
Reason: OP is on i386, my original java -version was wrong for F15
|

9th October 2012, 11:08 AM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 275

|
|
|
Re: java compile and run program help
Quote:
Originally Posted by marko
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.
|

9th October 2012, 12:09 PM
|
|
Registered User
|
|
Join Date: Apr 2005
Location: Finland
Posts: 5,076

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

9th October 2012, 11:18 PM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449

|
|
|
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; 9th October 2012 at 11:58 PM.
|

10th October 2012, 12:25 AM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 107

|
|
|
Re: java compile and run program help
I don't know but you might need to set JAVA_HOME env var.
|

10th October 2012, 04:47 AM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,619

|
|
|
Re: java compile and run program help
Quote:
Originally Posted by COKEDUDE
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:
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.
|

10th October 2012, 04:57 AM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449

|
|
|
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:
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 05:00 AM.
|

10th October 2012, 11:30 PM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,619

|
|
|
Re: java compile and run program help
Quote:
Originally Posted by marko
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 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
|

11th October 2012, 01:00 AM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,449

|
|
|
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 12:58 AM.
|

11th October 2012, 02:00 AM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,619

|
|
|
Re: java compile and run program help
Quote:
Originally Posted by marko
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 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 00:28 (Friday, 24-05-2013)
|
|
 |
 |
 |
 |
|
|