Installed Java SE Development Kit, but can't use the command javac - java

I've just installed the Java SE Development Kit from http://www.oracle.com/technetwork/java/javase/downloads/jdk10-downloads-4416644.html
I've created a test java file, to make into a Java class and run - the code is the following:
public class Hello {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
I then saved this as Hello.java and fired up Terminal (i'm on Mac OSX).
I simply type in:
javac Hello.java
and it returns the following error message:
-bash: javac: command not found
If anyone knows why and could help me fix the issue that'd be fantastic.

Download the right option. According to the download link, you're using an Oracle JDK. As a macOS user, you should choose the option dk-10.0.1_osx-x64_bin.dmg to download.
Ensure JDK 10 is installed. By default, Oracle JDK is installed in system directory /Library/Java/JavaVirtualMachines/. Browse it and check if there's a sub-directory called jdk-10.jdk:
$ ls /Library/Java/JavaVirtualMachines/
jdk-10.jdk jdk-11.jdk jdk-9.0.4.jdk jdk1.8.0_131.jdk
Check Java Control Panel. Go to "System Preferences" > "Java" to check the Java version installed.
Check the PATH variable. Ensure /usr/bin/ is included in it.
$ type javac
javac is /usr/bin/javac
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin
^

Check your %PATH% variable. Your %PATH% variable must have path to the bin directory in your JDK's installation directory. The PATH variable allows you to access certain files without having to specify their full path, you can check how to add JDK to your PATH here. If you do not want to mess with it, all you have to do is either cd into bin directory of your JDK installation or always specify the full path to javac, the choice is yours.

Related

JDK installed but no javap?

I have the JDK 10.0.2 installed, have the cmd opened in the folder of the class files of my program and try the command: javap Simulation.class .
I get the error that the command javap is not found. What do I do wrong?
You must have your $JAVA_HOME/bin directory added to system PATH for javap command to be available without it's absolute path.
Alternatively you can call javap using the absolute path:
/usr/lib/.../bin/javap Simulation.class
Assuming you are on Windows, check in your environment variable PATH whether path to Java executables is set.
Ex. If you have installed Java on path,say, C:\Program Files\Java\jdk-10
Then you have to add C:\Program Files\Java\jdk-10\bin to your PATH environment variable to be able to execute java commands from anywhere on command line.
References for Oracle Docs
https://docs.oracle.com/javase/10/install/installation-jdk-and-jre-microsoft-windows-platforms.htm#JSJIG-GUID-DAF345BA-B3E7-4CF2-B87A-B6662D691840
https://www.java.com/en/download/help/path.xml
On a sidenote, recommend you to move to Java 11 or 17 now that Java 10 is no longer supported. The above instructions would still remain same.
append $JAVA_HOME/bin to system $PATH variable. for example on linux
JAVA_HOME= "path of java installation on system"
export PATH = $JAVA_HOME/bin:$PATH
Open the control panel and type envir in search bar.
Click on edit the system environment variables.
Click on environment variables in the advanced system properties.
Click on path mentioned in user variables.
Edit the path, and click new and paste the path of java jdk.
In my system, the path is C:\Program Files\Java\jdk-17.0.2\bin, it could be different for your system.
Click ok, the problem must be solved now.

Setting JAVA_HOME variable

I try to set JAVA_HOME variable on an ubuntu server. I get the Java path with this command
which java
/usr/bin/java
I set the result in /etc/environment
JAVA_HOME="/usr/bin/java"
When I try to run a mvn command I get this error
Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/bin/java/bin/java
You should not set JAVA_HOME to /usr/bin/java, because that's just a symbolic link to the java executable, which points to where the real executable is.
JAVA_HOME should point to the Java installation directory, and not to the java executable (or a link to the executable).
Find out where your Java installation directory is and then set JAVA_HOME to that directory (and not to the java executable). If you installed Java using Ubuntu's package management system, then the Java home directory is probably one of the subdirectories in /usr/lib/jvm.
Per the Oracle site:
export JAVA_HOME=jdk-install-location
export PATH=$JAVA_HOME/bin:$PATH
You can add these lines into your ~/.bash_profile (or ~/.bashrc), and then refresh using source ~/.bash_profile

How to install java 7 on mac in custom location?

I want to install java 7 on mac silently. I am unable to find any documentation/links on the same. Also I don't want it in /Library. Is it possible to install the same on any custom location. I am very new to mac any help is highly appreciated.
Just to make the steps from #HawkMage more explicit (and illustrate them working with JDK8):
Download the binary (eg, jdk-8u5-macosx-x64.dmg) from Oracle
Double click from Finder to mount the Volume. Ignore the window with the “JDK 8 Update x.pkg”
Use pkgutil to expand the contents of the package into a temporary directory:
$ pkgutil --expand /Volumes/JDK\ 8\ Update\ 05/JDK\ 8\ Update\ 05.pkg /tmp/jdkpkg
Then, change to that dir and use cpio to expand the Payload file:
$ cd /tmp/jdkpkg
$ cpio -i < ./jdk18005.pkg/Payload
Finally, move the Home dir to wherever you’d like your JAVA_HOME to live
$ mv Contents/Home /mytools/jdk-1.8.0_05
Unfortunately the "standard" Java that comes on OS X is packaged in a very non-standard way.
It is not as easy as linux, the DMG downloaded gives you a PKG file that if you run it just installs Java. This is not useful if you are trying to keep the standard Java that comes with OS X intact.
What I do is download the DMG file from Oracle and open it but instead of running the PKG I use pkgutil to extract the contents of the package. You will find a directory named jdk*.pkg and in it you will see a file named Payload. This is a GZipped CPIO file and you can extract it by cating it and piping it into cpio -zi. From this you will now have a directory named Contents and under it you will find a directory named Home. This "Home" directory is the what you would normially get with the Linux tar.gz Java download. You can copy it to wherever you want and put the bin directory in your path and set the JAVA_HOME to it and you are good to go.
Just like in Linux, you can pretty much install Java anywhere you like on a mac. You just need to make sure that you add the Java executable to the path or create a symbolic link of the java executable and put it in the /usr/bin/ directory so it can be executed anywhere.
To add Java to path:
1) Modify .bash_profile found in your home director.
2) Add this line: export PATH=/yourjavadir/bin:$PATH
3) Save and exit
4) Then do source .bash_profile to reload the file. You'll only need to do this one time.
To create a symbolic link:
ln -s /yourjavadir/java /usr/bin/java
I had the same issue and just managed to figure it out.
Download and unzip the Java binary in your custom directory. For eg -
/Users/myuser/Documents/jre1.8.0_25.jre
Update your .bash_profile with the following parameters
export JAVA_HOME=/Users/a514624/Documents/jre1.8.0_25.jre/Contents/Home
export PATH=$PATH:$JAVA_HOME/bin
Close the terminal window and open it again. Alternately, you could type the command 'source .bash_profile'.
After these steps, if you type java -version on the command prompt, you would see it reflecting the version which you were hoping to see -
$ java -version
java version "1.8.0_25" Java(TM) SE Runtime
Environment (build 1.8.0_25-b17) Java HotSpot(TM) 64-Bit Server VM
(build 25.25-b02, mixed mode)
Unlink the existing Java softlink (would require root/elevated privileges)
root$ unlink /usr/bin/java
Create a symbolic link to the new Java installation
root$ ln -s /Users/myuser/Documents/jre1.8.0_25.jre/Contents/Home/bin/java /usr/bin/java
Thats it. Life is beautiful after this. Hope this helps!
If you just need JRE/Java Runtime Edition then previous answers arecorrect but if you need JRE and JDK (Java Development Kit) then simply go to below link and select the mac and run the dmg and it's much better and by far the easiest.
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

java version in linux

# which java
/usr/bin/which: no java in (/usr/local/jdk/jdk1.5.0_10/bin/java:/usr/local/jdk/jdk1.5.0_10/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin)
I installed java in /usr/local/jdk/jdk1.5.0_10 but cannot run java -version
I get this
$ java -version
-bash: /usr/bin/java: No such file or directory
this is red hat linux
It's not in your path.
use
export PATH=$PATH:/usr/local/jdk/jdk1.5.0_10/bin
export JAVA_HOME=/usr/local/jdk/jdk1.5.0_10
Look at ~/.bash_profile for where to define this permanatly.
Can you do
ls /usr/local/jdk/jdk1.5.0_10/bin
And why is /usr/local/jdk/jdk1.5.0_10/bin/java in your path?
Must be in local path of java before running to see what version is installed. Use find / -name java (this will list anything with java on the server copy the path ). path should end with..../jre/bin/java
copy full path.
Open Java location of the path you copied above, remove java at the end you path then enter,
Enter command below:
./Java -Version

"java -version" not working in command prompt

I downloaded this Java JDK and installed it.
But when I go to the command prompt to check the version, it says it's not recognized.
Is anyone else experiencing this issue with the latest Java?
I might not have installed the right version. I need the java that works with grails
C:\>java
'java' is not recognized as an internal or external command,
operable program or batch file.
C:\>java -version
'java' is not recognized as an internal or external command,
operable program or batch file.
C:\>
when i do a search on my computer for java, it does not find anything
Windows 2k8 R2 server-
For both java [-option] or %JAVA_HOME% to work in the command line you need the following:
In Control Panel->System and Security->System->Advanced system setting->Advanced->Environment Variables->System Variables
Edit the Path variable and add a ";" after the last value and add the the path to the Java bin directory:
e.g.- C:\Progra~2\Java\jre6\bin\
Add the JAVA_HOME Variable with the value set to the path for the java executable:
e.g.- C:\Progra~2\Java\jre6\bin\java.exe
Press simultaneously the "windows" and "pause" buttons on your keyboard, this will bring up the System Preferences dialog.
In the Advanced tab, find Environment Variables.
Then, in the User (upper) section, create or update the following two variables :
JAVA_HOME = where you put your JDK, eg. C:/Java/SDK
PATH = %JAVA_HOME%/bin
Close the dialogs.
Then, in a new command-line console, try "javac -version" and see if it's detected.
It's important that you use a new console, because environment variables are read only when the console is launched.
Java is typically installed (on Windows) as C:\Program Files\Java\jdk<version>
That installation directory has a subdirectory bin which you need to append to your PATH environment variable via the control panel. Then, the commands like java, javac etc. will be available on the command line.
BTW, the same is true for Grails.
Is the -version flag not recognized, or is the "java" command not recognized? One way to test this is just to type 'java' by itself and run it and see what happens.
If the command is not recognized, make sure that the JDK's install path is in your windows PATH. If not, you won't be able to use any of the java executables from the command prompt. Here's another link that may help out.
You need to manually add the path to javac.exe and java.exe to your operating system path. The Java installation program doesn't do that for you.
You most likely don't have java.exe in your system's PATH variable.
For Linux:
check $PATH and $JAVA_HOME. You can configure it in /etc/environment
From console you can check it like:
$ echo $PATH
For Windows:
My Computer -> Properties -> Advanced -> Environment Variables
Check there PATH.
From console you can check it like:
echo %PATH%
You should have a Java icon in Windows Control Panel. Locate the Java tab and click the View button. That will show you the path to the Java executables.
Last but not least, make sure you have restarted the computer so changes in the PATH variable can take effect.
You installed the JDK. Isn't java.exe part of the JRE? Do you have that installed?
Maybe your system variables in the environment variables are not set properly. Follow the steps in the link below. Finally, make sure the path component in system variables has only one JDK path. Delete other JDK paths that you won't use.
https://docs.oracle.com/javase/tutorial/essential/environment/paths.html
You might have installed a previous or older version of Java so you can just uninstall it and directly download JDK from Java JDK
I was also facing the same issue but when I downloaded and installed the latest version the issue was resolved.
Also uninstall any other Java versions which may be present in the program files.
To get to know other versions which may be present in your computer use Windows File Explorer to go to:
Files > Windows C > Program Files > Java
All the JDK and JRE you have will be listed there. Uninstall any other Java versions other than what you installed. And then make sure you have set the path variable of the JDK.
For me, it was incorrect line in the PATH
(1) Check PATH: Type "Edit System Environment Variables" on the search -> System Properties -> Advance -> Environment Variables -> System variables -> Path
(2) On the list, mine was C:\Program Files (x86)\Common Files\Oracle\Java\javapath so I just add \ at the end (the exe files in the javapath folder)
OS: Windows 10 64 bit
IF you set the PATH and it's not showing up in cmd when you run %PATH%, try restarting your computer.

Categories