Setting JAVA_HOME when running Ant from Java - java

The reason is long and boring, but I need to run an Ant script to compile Java 1.5 code from a Java 1.4 app. I keep getting this error, though:
BUILD FAILED
build.xml:16: 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 "C:\j2sdk1.4.2_16\jre"
In my code, I have:
Project p = new Project();
p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.setProperty("java.home", "C:\Program Files\Java\jdk1.6.0_04");
p.fireBuildStarted();
p.init();
// so on and so forth
but it ignores it. I've also tried p.setUserProperty(String, String), but that doesn't do the trick, either. Is there a way to do it without launching a separate process?

Does the javac task in your buildfile have fork="yes"? If not, then it doesn't matter what the java.home property is set to; ant will attempt to call the javac Main method in the same java process, which from your error is a JRE, not a JDK.
EDIT Try setting the executable property of your javac task to the full path to the javac binary and add compiler="extJavac" to the task.

Shouldn't the backslashes be doubled?
p.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.6.0_04");

Have you set environment variables JAVA_HOME and ANT_HOME properly? If you are setting via code it should work though.
Also check if your %JAVA_HOME%\bin directory %ANT_HOME%\bin should be in the environment variable 'path'.
Your problem seems to be with the %JAVA_HOME%\bin not being present in the envt. variable path though.

Another way to make this work is to add 'tools.jar' to your classpath. The javac compiler is contained within this jar.
java -cp $JAVA_HOME/lib/tools.jar ...

javac option is available in tools.jar. In eclipse, even if your JRE HOME points to a jdk, all the system libraries point to JDK_HOME\jre\lib. There is no tools.jar. You can add tools.jar as an external Jar file. This should solve your issue

Related

On Windows 8.1 "javac -version" works but Cordova doesn't recognize it

I'm trying to set up Cordova. When I run cordova build android I receive the following error:
(node:6816) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Failed to run "javac -version", make sure that you have a JDK installed.
You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
Your JAVA_HOME is invalid: C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin
However I can run javac -version just fine.
C:\>javac -version
javac 1.8.0_161
I've tried to set JAVA_HOME in various ways: pointing to the root; pointing to the bin folder; set it as a User variable and also a System one; add %JAVA_HOME%\bin to Path (both for User and System) and so on.
I've checked several articles about this issue, and they say that if I'm able to use javac -version then that's supposed to indicate that my environmental variables are set correctly. Even if that's the case, Cordova still doesn't seem to recognize it.
Update
Following Stephen C's instructions, I've reset my variables, yet my error still persists. As you can see in the image I can call javac just fine, and both JAVA_HOME and Path seem to be set in the right way. (Note that actually it is %JAVA_HOME%\bin and not C:\Program Files\Java\jdk1.8.0_161\bin in the Path.)
I think you are confusing JAVA_HOME, PATH and CLASSPATH
The JAVA_HOME variable should be set to the name of a single directory. Not multiple directories with separators. The JAVA_HOME directory is the top directory of your JDK or JRE installation. Not the "bin" directory.
The PATH and CLASSPATH variables consist of a list of paths. For PATH the paths are pathnames for directories. For CLASSPATH the paths are pathnames for directories or JAR / ZIP files (or a particular kind of wildcard).
The PATH is where the shell searches for commands if you use a command name that is a simple name; e.g. java or javac.
`
The CLASSPATH is one of the ways that you can tell Java tools to look cor compiled classes to load, compile against, etcetera.
Your error message says this:
Your JAVA_HOME is invalid:
C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin
This is wrong for two reasons:
JAVA_HOME should not be a path
C:\Program Files\Java\jdk1.8.0_161\bin is not a Java home. The Java home is C:\Program Files\Java\jdk1.8.0_161 .... probably.
The other thing you may have gotten wrong is that changes to environment variables do not necessarily propagate. For example, if you start a shell and then change an environment variable via the Windows GUI, the change may not propagate to the shell. You may need to exit the shell and restart it to pick up the new value.
If you are unsure, you can run (for example) echo %PATH% to see the current value of PATH in the current shell or script.
UPDATE
According to http://cordova.apache.org/docs/en/7.x/guide/platforms/android/index.html, you need to:
install Android Studio
set the ANDROID_HOME environment variable to the location of your Android SDK installation.
It is also recommended that you add the Android SDK's tools, tools/bin, and platform-tools directories to your PATH.
Remove java.dll from C:\Windows\System32 (if any).
I'm not sure but It may work correctly.

JAVA_HOME with JDK8 and JDK9

For some reasons I need to use JDK8 and JDK9. Is a good idea to put both paths (to JDK8 and JDK9) into the same JAVA_HOME system environment's variable?
Details: I need to run both systems at the same time, one with ant (which uses jdk8) and second with maven (which uses jdk9).
Usually that path is reserved for the current active java command keyword in the command line interface. You can't have multiple JDK active at the same time at any moment when using the terminal. So it is not a good idea.
You can however point the JAVA_HOME to the folder where you have multiple JDK installations and then set the PATH variable to a certain JDK. So when you want to change the JDK you change only the PATH variable and leave JAVA_HOME as it is.
If you intend to use different JDK across multiple projects in an IDE, then yes you can have multiple JDK and you can chose the JDK you want to use in the Project Settings.
If I were you then I would create function() in .profile or .bashrc for command prompt or terminal which will export JAVA_HOME variable to Java8 or Java9 depending on whether I am running ant or mvn respectively.
Lets say your Java8 and Java9 are installed at below locations ...
C:\Program Files\Java\jdk1.8.0_151\bin
C:\Program Files\Java\jdk1.9.0_4\bin
Then your functions in .profile or .bashrc should be like this ...
For ant and Java8 (here i am passing command line argument $1 to ant command)...
runant() {
export JAVA_HOME="C:\Program Files\Java\jdk1.8.0_151\bin";
ant $1;
}
For mvn and Java9 ...
runmvn() {
export JAVA_HOME="C:\Program Files\Java\jdk1.9.0_4\bin";
mvn clean install;
}
With the above functions, you can run ant and mvn from command prompt and JAVA_HOME will be set appropriately ONLY for that specific run.

Issue while running ant command

I installed ant in my system, when I run ant command, I get following error:
Error: JAVA_HOME is not defined correctly. We cannot execute
/usr/lib/jvm/java-6-sun/bin/bin/java
Please suggest what should I do?
Set the environment variable JAVA_HOME to the installation directory of your JDK, which is: /usr/lib/jvm/java-6-sun
It looks like you have set it to /usr/lib/jvm/java-6-sun/bin (note the /bin at the end); the ant command appends another /bin so that you get .../bin/bin/java which is incorrect.
In your Java installation directory, u can see a jre subdirectory that contains the bin directory that contains java and related executables.
So you should probably be setting JAVA_HOME to /usr/lib/jvm/jdk/jre rather than /usr/lib/jvm/jdk.

setting up classpath. javac is not recognized

I am trying to run my java program from command line.
I read an article about setting up classpath, but I get an error of javac is
not recognized as internal or external command. What should I do? (I dont want to set a permanent CLASSPATH)
This is what I have done in my command line
D:\user> set path=%path%;C:\Program Files\Java\1.7.0_07\bin
D:\user> cd testing
D:\user\testing> javac firstProgram.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
Thank you
Assuming that the PATH is correct1, the most likely cause is that you have a JRE installation ... and a JRE doesn't include a java compiler. You need a JDK installation if you want to compile from the command line.
(You can confirm this by looking in the C:\Program Files\Java\1.7.0_07\bin directory to see if it contains a javac.exe file. A JRE won't ...)
Where can I find the Java compiler to download..
You need to download one of the JDK installers; see http://www.oracle.com/technetwork/java/javase/downloads/index.html
1 - I don't think quotes are required in a PATH variable on Windows. At least that's what various examples that Google found for me seem to imply. But I've never really understood the logic behind quoting in Windows ...
Its an issue related to Program Files.
First make sure that your JDK Folder is installed in Program Files or Program Files(x86) or any other folder.
Then you should use the path of bin folder in " ". Because command prompt does break the string at space. When you will write it in " " then it will take is as a whole String.
You try these commands
set path=%path%;"C:\Program Files\Java\1.7.0_07\bin"
or
set path=%path%;"C:\Program Files(x86)\Java\1.7.0_07\bin"
It might help you to get out of this.
Better do it in Environmental variable and check it!
try below command is recognized from command prompt
C:\Program Files\Java\1.7.0_07\bin\javac ab.java
This is just to verify your javac
Here's how you can set the path temporary, meaning if you close and reopen "command prompt" you will have to set the path again.
Assuming the path is C:\Program Files\Java\jdk1.6.0\bin
TYPE IN C:\Program Files\Java\jdk1.6.0\bin AND HIT ENTER
that's it.
The commands D:\user> set path=%path%;C:\Program Files\Java\1.7.0_07\bin works well for me
Adding few more information to this:
Please check the version of JDK and JRE installed on your computer. Recently I faced the same problem even after setting the PATH. It gives the error "javac - command is not recognised"
Solution is there must be similar versions of JDK as well as JRE
E.g.: JDK 1.7.75 along with JRE 1.7.75

Cannot compile a Java program command-line

I tried the following in cl:
javac Main.java
The shell gives an error that "javac" is not a recognized command. I know for a fact I have the Java Platform installed. What could be wrong?
(I use "cd" to set the directory first)
javac is most likely not on the PATH, and you must give the full path in your command.
Add /bin folder to your environment variables.
How do I set or change the PATH system variable?

Categories