- Ant unable to find javac, JAVA_HOME won’t set on Ubuntu
- 4 Answers 4
- How to Run Java Programs in Ubuntu
- Running Java programs in Ubuntu
- Step 1: Install Java compiler
- Step 2: Compile Java program in Linux
- Step 3: Run the Java class file
- Unable to find javac after installing OpenJDK 1.6 and 1.7
- 6 Answers 6
- Where can I find the Java SDK in Linux after installing it?
- 14 Answers 14
Ant unable to find javac, JAVA_HOME won’t set on Ubuntu
I have an Android Project called Hello on my Ubuntu 10.04 i386 Server (headless). It contains all things an Android project folder should have. I first build the project in bash while in the Project folder using this synax:
./android create project --target 5 --name HelloCompile --path ../../Projects/Hello --activity HelloActivity --package com.code.Hello
BUILD FAILED /home/myusername/www/sdk/tools/ant/main_rules.xml:384: Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK. It is currently set to "/usr/lib/jvm/java-6-openjdk/jre"
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
does not fix the problem either — I get the same error. Nothing I do changes the fact Ubunut still thinks /usr/lib/jvm/java-6-openjdk/jre is the JAVA_HOME. What is going wrong? I’ve been at this for too many hours.
4 Answers 4
When you install Ubuntu only the JRE is installed as part of the default packages. Unfortunately Ubuntu’s package management names the directory as if the JRE were installed along with the JDK. The directory is named java-6-openjdk even though the JDK is not be present.
sudo apt-get install openjdk-6-jdk
It will install the JDK in that same directory.
oh god. I can’t believe I missed that — I was sure I installed it. I have no idea how the JDK got there in the first place then. that’s really strange. anyway, thanks for getting it right. I’m actually sorry — this was such a silly mistake
note: the openJDK are not headless. This solution will install a bit chunk of GUI on your server, whether you need it or not. But it is an easy solution 🙂
— Updated after noticing a small item in your output —
You have your JAVA_HOME set to the correct location for a Java Runtime Environment, which unsuprisingly will allow you to run Java programs, but not develop them.
Shorten your JAVA_HOME to /usr/lib/jvm/java-6-openjdk (note the removal of the trailing jre ). After that your Ant wrappers / compiler detection code won’t get confused, as it will be pointing to the home of your Java Development Environment instead of the embedded, related Java Runtime Environment.
The embedded Java Runtime Environment is provided to make sure you can test against just the core (compiler tools not included) Java offerings.
Finding the command javac has little to do with JAVA_HOME beyond that javac is typically found in a subdirectory under JAVA_HOME
What you need to do is to modify your PATH environmental variable to include the directory where the Java executables are located. Typically this is done like so
but it might be done slightly differently depending on your setup. If you do
and you see a javac executable, then the above modification of the path variable will work without any need to change it.
How to Run Java Programs in Ubuntu
So, you have started learning Java programming? That’s good.
And you want to run the java programs on your Linux system? Even better.
Let me show how to run Java in terminal in Ubuntu and other Linux distributions.
Running Java programs in Ubuntu
Let’s go in proper steps here.
Step 1: Install Java compiler
To run a Java program, you need to compile the program first. You need Java compiler for this purpose.
The Java compiler is part of JDK (Java Development Kit). You need to install JDK in order to compile and run Java programs.
First, check if you already have Java Compiler installed on your system:
If you see an error like “Command ‘javac’ not found, but can be installed with”, this means you need to install Java Development Kit.
The simplest way to install JDK on Ubuntu is to go with the default offering from Ubuntu:
sudo apt install default-jdk
You’ll be asked to enter your account’s password. When you type the password, nothing is seen on the screen. That is normal. Just enter your password blindly. When asked, press the enter key or Y key.
The above command should work for other Debian and Ubuntu based distributions like Linux Mint, elementary OS etc. For other distributions, use your distribution’s package manager. The package name could also be different.
Once installed, verify that javac is available now.
Step 2: Compile Java program in Linux
You need to have a Java program file for this reason. Let’s say you create a new Java program file named HelloWorld.java and it has the following content:
You can use Nano editor in terminal or Gedit graphical text editor for writing your Java programs.
If there is no error, the above command produces no output.
When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file.
Step 3: Run the Java class file
You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac.
This will print Hello World on the screen for my program.
And that’s how you run a Java program in the Linux terminal.
This was the simplest of the example. The sample program had just one class. The Java compiler creates a class file for each class in your program. Things get complicated for bigger programs and projects.
This is why I advise installing Eclipse on Ubuntu for proper Java programming. It is easier to program in an IDE.
I hope you find this tutorial helpful. Questions or suggestions? The comment section is all yours.
Unable to find javac after installing OpenJDK 1.6 and 1.7
. and I came up with nothing. I searched the web for answers but they all seem to focus on javac existing on the filesystem, and that needs to be done is to restore a missing symlink. Has anyone run up against this issue and found a solution? I have attempted to install openJDK on Scientific Linux 6.3 with the command:
6 Answers 6
You actually just need to install java-[version]-openjdk-devel , because the base package doesn’t include compilers or other developer tools. This contains /usr/lib/jvm/java-[version]-openjdk-[version].[arch]/bin/javac , and will use the alternatives system to set up a symlink to /usr/bin/javac .
If you receive this error, UNIX cannot find the compiler, javac.
Here’s one way to tell UNIX where to find javac. Suppose you installed the JDK in /usr/local/jdk1.8.0 . At the prompt you would type the following command and press Return:
/usr/local/jdk1.8.0/javac hello.java
Note: If you choose this option, each time you compile or run a program, you’ll have to precede your javac and java commands with /usr/local/jdk1.8.0/ . To avoid this extra typing, you could add this information to your PATH variable. The steps for doing so will vary depending on which shell you are currently running.
Class names, ‘hello’, are only accepted if annotation processing is explicitly requested
If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac hello.java not javac hello.
Where can I find the Java SDK in Linux after installing it?
I installed JDK using apt-get install but I don’t know where my jdk folder is. I need to set the path for that. Does any one have a clue on the location?
14 Answers 14
This depends a bit from your package system . if the java command works, you can type readlink -f $(which java) to find the location of the java command. On the OpenSUSE system I’m on now it returns /usr/lib64/jvm/java-1.6.0-openjdk-1.6.0/jre/bin/java (but this is not a system which uses apt-get ).
On Ubuntu, it looks like it is in /usr/lib/jvm/java-6-openjdk/ for OpenJDK, and in some other subdirectory of /usr/lib/jvm/ for Suns JDK (and other implementations as well, I think).
For any given package you can determine what files it installs and where it installs them by querying dpkg. For example for the package ‘openjdk-6-jdk’: dpkg -L openjdk-6-jdk
I think readlink as given is most elegant (and efficient), but I suggest readlink -f $(which javac) — note the ‘c’ in case there is a strange mix of JDK and JRE on the same machine. The JDK includes a compiler (javac) and a JRE does not. However if path is not correct, which will fail and you can try this: find /usr/java -wholename ‘*ava/jdk*’ -prune as I note in a comment below.
will tell you which java implementation is the default for your system and where in the filesystem it is installed. Check the manual for more options.
@dequis, it’s an answer specific to debian, since the question mentioned apt-get and the distro to be Debian 2.6.26 . AFAIK, it should be the same for all debian derivatives.
should give you something like
This does not actually point to a full JDK. 1. It is a symlink, and even if you read the symlink, the binary is also not within a JDK. For example, if I run the command readlink -f $(which javac) it prints /usr/lib/jvm/java-8-oracle/bin/javac . That bin folder is NOT a JDK. General acid-base test to see if its a JDK is to see if the current $JAVA_HOME contains a path of lib/tools.jar . In the cast of /usr/lib/jvm/java-8-oracle/bin that is not true, therefore it is not a JDK.
On Centos / RHL This is what I prefer to find the JDK (if installed) find /usr/java -wholename ‘*ava/jdk*’ -prune But behavior depends whether you are talking about OpenJDK or Oracle Java and how it was installed in the first place.
This question will get moved but you can do the following
«find / -name ‘javac'» is less typing, but requires admin (root) privilege or you will get a lot permission denied messages.
Use find to located it. It should be under /usr somewhere:
When running the command, if there are too many «Permission denied» message obfuscating the actual found results then, simply redirect stderr to /dev/null
find /usr -name java 2> /dev/null
Another best way to find Java folder path is to use alternatives command in Fedora Linux (I know its for Ubuntu but I hit this post from google just by its headline). Just want to share incase people like me looking for answers for fedora flavour.
To display all information regarding java
alternatives --display java
Three Step Process: First: open Terminal-> $ whereis java it would give output like this: java: /usr/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
Second: ls -l /usr/bin/java It would give output like this: lrwxrwxrwx 1 root root 22 Feb 9 10:59 /usr/bin/java -> /etc/alternatives/java
Third: ls -l /etc/alternatives/java output is the JDK path: lrwxrwxrwx 1 root root 46 Feb 9 10:59 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
It’s /usr/local/java/jdk[version]
This question still seems relevant, and the answer seems to be a moving target.
On my debian system (buster):
> update-java-alternatives -l java-1.11.0-openjdk-amd64 1111 /usr/lib/jvm/java-1.11.0-openjdk-amd64
However, if you actually go look there, you’ll see there are multiple directories and symbolic links placed there by the package system to simplify future maintenance.
The actual directory is java-11-openjdk-amd64 , with another symlink of default-java . There is also an openjdk-11 directory, but it appears to only contain a source.zip file.
Given this, for Debian ONLY, I would guess the best value to use is /usr/lib/jvm/default-java , as this should always be valid, even if you decide to install a totally different version of java, or even switch vendors.
The normal reason to want to know the path is because some application wants it, and you probably don’t want that app to break because you did an upgrade that changed version numbers.