PDA

View Full Version : Problems compiling Java in a terminal



tech291083
28th May 2007, 04:37 PM
Hi,

I am on Fedora core 5 32+ bit and just installed JDK 6u1 with NetBeans 5.5.1 but when I try to compile a small file called james.java saved in a folder called PracJava on desktop, from the terminal with the following commands it for some reason gives the following error:



[root@localhost ~]# cd Desktop/PracJava
[root@localhost PracJava]# javac james.java
/usr/bin/javac: line 3: java: command not found
[root@localhost PracJava]#


Here is the cod of the file named james.java


public class google
{
public static void main(String args[]) {
System.out.println("Hello Mr. Bond, nice to see you!");
}
}


Can some one suggest me something?

Thanks

tech291083
28th May 2007, 04:43 PM
i just did the following


[root@localhost ~]# su
[root@localhost ~]# whereis netbeans
netbeans: /opt/netbeans-5.5.1/bin/netbeans
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
jdk1.6.0_01 jre-6u1-linux-i586-rpm.bin netbeans-5.5.1
jre-6u1-linux-i586.rpm libjavaplugin_oji.so
[root@localhost opt]#


One thing here is strange, libjavaplugin_oji.so is flashing in a red background. Is it some kind of error that needs to be corrected?

Thanks...

baschti
28th May 2007, 05:03 PM
One thing here is strange, libjavaplugin_oji.so is flashing in a red background. Is it some kind of error that needs to be corrected?
its an orphan'd symbolic link - so the target it points to is moved or deleted

tech291083
28th May 2007, 05:08 PM
its an orphan'd symbolic link - so the target it points to is moved or deleted

Thanks,

you are very right, I had install JRE alone yesterday and given a path. So what do I do next in order to compile Java without any problems.

baschti
28th May 2007, 05:10 PM
..so i guess your java installation is broken
and - i'm not sure but i guess the name of the class should coincide with the filename, so you should rename the file to google.java

baschti
28th May 2007, 05:16 PM
and the error message "/usr/bin/javac: line 3: java: command not found" looks like /usr/bin/javac is a script that calls "java" - which is not found

tech291083
28th May 2007, 05:17 PM
..so i guess your java installation is broken
and - i'm not sure but i guess the name of the class should coincide with the filename, so you should rename the file to google.java


Thanks again,

just changed the file name to google.java as class is named java in the code, but still the same error. Any other suggestions. Thanks

baschti
28th May 2007, 05:32 PM
can you please post the output of
echo $PATH and
which java javac
(i guess with java and/or javac you are not calling the version you've installed inside /opt)

tech291083
28th May 2007, 05:44 PM
can you please post the output of
echo $PATH and
which java javac
(i guess with java and/or javac you are not calling the version you've installed inside /opt)

Thanks here it is



[root@localhost ~]# su
[root@localhost ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@localhost ~]# which java javac
/usr/bin/which: no java in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin)
/usr/bin/javac
[root@localhost ~]#

baschti
28th May 2007, 06:09 PM
i meant the PATH variable off your 'normal' user, not root - but i think i know the problem:
inside /opt/jdk1.6.0_01 (i guess you've installed java there) should be a directory where the java binarys are
i guess it is something like /opt/jdk1.6.0_01/bin
once you've found that directory (i call it javadir) do a
export PATH=javadir:$PATH
if you can compile now, you are allmost done - just insert PATH=javadir:$PATH
bevore
export PATH
in your ~/.bash_profile

tech291083
28th May 2007, 06:20 PM
i meant the PATH variable off your 'normal' user, not root - but i think i know the problem:
inside /opt/jdk1.6.0_01 (i guess you've installed java there) should be a directory where the java binarys are
i guess it is something like /opt/jdk1.6.0_01/bin
once you've found that directory (i call it javadir) do a
export PATH=javadir:$PATH
if you can compile now, you are allmost done - just insert PATH=javadir:$PATH
bevore
export PATH
in your ~/.bash_profile


thanks, got your point a little bit and first tried this


[root@localhost ~]# whereis netbeans
netbeans: /opt/netbeans-5.5.1/bin/netbeans
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
jdk1.6.0_01 jre-6u1-linux-i586-rpm.bin netbeans-5.5.1
jre-6u1-linux-i586.rpm libjavaplugin_oji.so
[root@localhost opt]# ls /opt/jdk1.6.0_01
bin include man sample
COPYRIGHT jre README.html src.zip
db lib README_ja.html THIRDPARTYLICENSEREADME.txt
demo LICENSE README_zh_CN.html _uninst
[root@localhost opt]#


Yes you are very right there is sub directory called jdk1.6.0_01 which has the directory bin so there is something here that is /opt/jdk1.6.0_01/bin

So now all I need to do is go inside the bin directory under the jdk1.6.0_01 directory right? and then type exactly what at the prompt please?

tech291083
28th May 2007, 06:24 PM
So my command should look like this right?


[root@localhost opt]# cd /opt/jdk1.6.0_01/bin
[root@localhost bin]# export PATH=/opt/jdk1.6.0_01/bin:$PATH

baschti
28th May 2007, 06:32 PM
if inside this directory the java binarys like java and javac are, then yes
- but do this as your normal user, since the shell variables are set per user .. and you don't want to compile as root

tech291083
28th May 2007, 06:37 PM
if inside this directory the java binarys like java and javac are, then yes
- but do this as your normal user, since the shell variables are set per user .. and you don't want to compile as root

Thanks yes there are these two in it java and javac both I can see.


[root@localhost bin]# ls
appletviewer jar javap jhat jstack pack200 serialver xjc
apt jarsigner java-rmi.cgi jinfo jstat policytool servertool
ControlPanel java javaws jmap jstatd rmic tnameserv
extcheck javac jconsole jps keytool rmid unpack200
HtmlConverter javadoc jcontrol jrunscript native2ascii rmiregistry wsgen
idlj javah jdb jsadebugd orbd schemagen wsimport
[root@localhost bin]#

tech291083
28th May 2007, 06:41 PM
[root@localhost bin]# export PATH=/opt/jdk1.6.0_01/bin:$PATH
[root@localhost bin]# echo $PATH
/opt/jdk1.6.0_01/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@localhost bin]#

tech291083
28th May 2007, 06:48 PM
[root@localhost PracJava]# javac google.java
[root@localhost PracJava]# java google
Hello Mr. Bond, nice to see you!


Looks like its working, after changing the path, but do I always need to name the java file exactly as its class name inside?

tech291083
28th May 2007, 06:54 PM
[root@localhost ~]# alternatives --config java

There are 2 programs which provide 'java'.

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

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

Now how do I delete the first one here ie /usr/lib/jvm/jre-1.4.2-gcj/bin/java which is no longer needed as JRE has been deleted manually be me only.

baschti
28th May 2007, 07:05 PM
[root@localhost PracJava]# javac google.java
[root@localhost PracJava]# java google
Hello Mr. Bond, nice to see you!


Looks like its working, after changing the path, but do I always need to name the java file exactly as its class name inside?
hey, cool problem solved :-)
...but - as i said twice: please do not work as root if you don't have to! set the path variable as the normal user and compile as the normal user
i'm not that kind of a java expert, but i think if a java file contains a public class, then this class should be named like the java file

faustjoe
28th May 2007, 08:00 PM
I also have a problem with my java, I have the symbolic link but my java does not work. This is what the output when I type this command /usr/sbin/alternatives --config java

There are 7 programs which provide 'java'.

Selection Command
-----------------------------------------------
1 /opt/jre1.5.0_09/bin/java
2 /opt/jre1.5.0_06/bin/java
3 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
* 4 /usr/java/jrel.5.0_11/bin/java
5 /opt/jrel.5.0_11/binjava
6 /opt/jre1.5.0_11/bin/java
+ 7 /usr/java/jre1.6.0_01/bin/java

I think this may be the problem Can someone assist me to get my java to work

baschti
28th May 2007, 08:14 PM
hi faustjoe,

i assume that you are not a "professional" java programmer who needs to test his programms with several versions of java..
so the first i would do, is to get rid of the not needed java versions - i assume you've installed them using rpm's
so search for the java verions, which you have installed with rpm :
rpm -qa | grep "java\|jdk\|jre"
and erase some versions (maybe all, exept one from /opt) with "rpm -e"

faustjoe
28th May 2007, 08:23 PM
yeah I'm new to linux and had java working but not it is all messed up
after running that command and doing an rpm -e I get this

ouch: cannot touch `/usr/java/jre1.5.0_11/lib/rt.pack': No such file or directory
touch: cannot touch `/usr/java/jre1.5.0_11/lib/jsse.pack': No such file or directory
touch: cannot touch `/usr/java/jre1.5.0_11/lib/charsets.pack': No such file or directory
touch: cannot touch `/usr/java/jre1.5.0_11/lib/ext/localedata.pack': No such file or directory
touch: cannot touch `/usr/java/jre1.5.0_11/lib/plugin.pack': No such file or directory
touch: cannot touch `/usr/java/jre1.5.0_11/lib/javaws.pack': No such file or directory
touch: cannot touch `/usr/java/jre1.5.0_11/lib/deploy.pack': No such file or directory

baschti
28th May 2007, 08:39 PM
..never seen that berore - looks like you delete those files manualy, but the rpm database is not aware of that
the only thing i can recommend is to go through the rpm manpage (man rpm) and search for a ignore or force option

baschti
28th May 2007, 08:45 PM
..maybe thats the whole problem - you manualy deleted some java versions
maybe you could use "alternatives" to switch to an other (working) version of java

faustjoe
28th May 2007, 11:49 PM
not sure I understand what you mean when you say "alternatives" . I currently have jre1.6.0_01 and that file is still present and that is where the link is established.

faustjoe
29th May 2007, 01:37 AM
this is what I have so far, I removed old versions of java.

Selection Command
-----------------------------------------------
*+ 1 /usr/java/jre1.6.0_01/bin/java

Enter to keep the current selection[+], or type selection number: 1
[root@localhost java]# /usr/sbin/alternatives --display java
java - status is manual.
link currently points to /usr/java/jre1.6.0_01/bin/java
/usr/java/jre1.6.0_01/bin/java - priority 1
slave keytool: (null)
slave rmiregistry: (null)
slave jre_exports: (null)
slave jre: (null)
Current `best' version is /usr/java/jre1.6.0_01/bin/java.


but when I do a about : plugin in firefox it does not install the java plugin
not sure what the problem is.

baschti
29th May 2007, 09:13 AM
use the force..oh wait, the search function..
-i did...and found this (http://fedoraforum.org/forum/showthread.php?t=135568)

tech291083
29th May 2007, 11:09 AM
hey, cool problem solved :-)
if a java file contains a public class, then this class should be named like the java file

Thanks a lot but whenever I restart the pc or open up the terminal when it is already on I have to change the path every time. Then only it starts working. Why so? Can't there be a permanent solution to this dilema? Your help so far has been fantastic and bang on target which I really appreciate. But how do I get rid of the first one here ie /usr/lib/jvm/jre-1.4.2-gcj/bin/java which is no longer needed as JRE has been deleted manually be me only.



[root@localhost ~]# alternatives --config java

There are 2 programs which provide 'java'.

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

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

Thanks...

baschti
29th May 2007, 11:26 AM
Thanks a lot but whenever I restart the pc or open up the terminal when it is already on I have to change the path every time. Then only it starts working. Why so? Can't there be a permanent solution to this dilema?
i answered this in post 10 of this thread:

if you can compile now, you are allmost done - just insert PATH=javadir:$PATH
bevore
export PATH
in your ~/.bash_profile

baschti
29th May 2007, 11:33 AM
But how do I get rid of the first one here ie /usr/lib/jvm/jre-1.4.2-gcj/bin/java which is no longer needed as JRE has been deleted manually be me only.
..i hope with manually deletion you mean deletion using rpm
i would recommend you to keep this version...but i'ts too late ;-(
if you are sure that you don't need it, read: http://fedoraforum.org/forum/showthread.php?t=152483&highlight=gcj

tech291083
29th May 2007, 12:03 PM
i answered this in post 10 of this thread:

Yes I did delete the JRE installed before manually with the rpm -e command in the terminal. I did install JRE previuosly but since JDK with NetBeans has JRE built into it, I had to remove/delete it.
Thanks and my apolozies to you again as I really do not know how I can access and modify my bash profile. I need further assistance in this regard. But let me ask you first do I really need to delete any of the 2 entries at all?


*+ 1 /usr/lib/jvm/jre-1.4.2-gcj/bin/java
2 /pathto/sunjava/java


As I am not sure what each of them is required for. I must say that during the period of communicating with you on this thread I have learned a few things and credit goes to you.

tech291083
29th May 2007, 12:07 PM
I just did:


[root@localhost ~]# vi .bash_profile
The vi editor can help here but not sure though.

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
unset USERNAME
~
~
".bash_profile" 13L, 191C

baschti
29th May 2007, 12:34 PM
you don't have to use vi, if you are not familiar with
just use your editor of choice....emacs, gedit, kwrite....whatever

i assume that you've opened a terminal|console|shell..whatever, and that you are a non-root user!
now type
gedit ~/.bash_profile
and change the file that it looks like:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
PATH=javadir:$PATH
export PATH
unset USERNAME

giulix
29th May 2007, 12:38 PM
May I suggest you have a look at this (http://forums.fedoraforum.org/forum/showthread.php?t=155846) thread, too ? It provides instructions on how to make your Java installation the default one at login. This solution works for any Unix system. If you need a customized script, post your Java installation path and I'll provide you with one.

tech291083
29th May 2007, 12:54 PM
now type
gedit ~/.bash_profile
and change the file that it looks like:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
PATH=javadir:$PATH
export PATH
unset USERNAME

Thanks with gedit it should be easy. So in short all I need to change is add this line


PATH=javadir:$PATH

I mean in my case I shoule be adding this line into the bash profile


PATH=/opt/jdk1.6.0_01/bin:$PATH

Am I right?

tech291083
29th May 2007, 12:57 PM
This is how my modified bash profile looks now:


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin
PATH=/opt/jdk1.6.0_01/bin:$PATH
export PATH
unset USERNAME

tech291083
29th May 2007, 01:05 PM
if you need a customized script, post your Java installation path and I'll provide you with one.

Thanks here it is


/opt/jdk1.6.0_01/bin

giulix
29th May 2007, 02:45 PM
Ok, so paste this in a text file using your favorite editor


#!/bin/sh
export JAVA_HOME=/opt/jdk1.6.0_01
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/jpda.jar:$JAVA_HOME/jre/lib/rt.jar:.
alias java=$JAVA_HOME/bin/java
alias javah=$JAVA_HOME/bin/javah
alias javac=$JAVA_HOME/bin/javac
alias javap=$JAVA_HOME/bin/javap
alias javadoc=$JAVA_HOME/bin/javadoc
alias javadt=$JAVA_HOME/bin/javadt

Save the file naming it, for example, javapath.sh in your $HOME directory (/home/your_username)
Edit your .bashrc file, which you'll find in your $HOME directory (/home/your_username) and add the following line at the end of it


. ./javapath.sh

Save the file, logout and log back in.

faustjoe
2nd June 2007, 07:43 AM
Java is really getting on my nerves I have followed ever instruction I have link from the /opt directory to the plugin directory in firefox and opera and Java is not recognized as a plugin. There is no red blinking link to show broken link. I do not get it. when I type rpm -qa | grep "java\|jdk\|jre" I get this :

java_cup-0.10-0.k.6jpp.1
java-1.5.0-gcj-1.5.0.0-14.fc7
openoffice.org-javafilter-2.2.0-14.8