Correctly install Java for Matlab - java

I am trying to extract a jar file from Matlab code using javac from Library Compiler (java package).
I have set JAVA_HOME to:
C:\Program Files\Java\jdk1.7.0_71
and added to PATH:
C:\Program Files\Java\jdk1.7.0_71\bin.
When I enter java -version in my console, I get java version jdk1.7.0_71 and running javac -version shows jdk1.7.0_71.
However, it seems that matlab could not find javac, thus, I am not able to compile my .m code into a .jar file. When I tried to compile .m code I got the following:
Error: An error occurred while shelling out to javac (error code = -1).
Unable to build executable.
Executing command: ""C:\Program Files\Java\Java\jdk1.7.0_11\bin\javac" -verbose - classpath "...\MATLAB\R2014a\toolbox\javabuilder\jar\javabuilder.jar" -d "...\features_extraction\for_testing\classes" "...\features_extraction\for_testing\features_extraction\Class1.java" "...\features_extraction\for_testing\features_extraction\Features_extractionMCRFactory.java" "...\for_testing\features_extraction\Class1Remote.java" "...\for_testing\features_extraction\package-info.java""
This is the directory of my java bin folder and javac.exe exists : ...\Java\jdk1.7.0_71\bin\bin\javac? I found this link for compatibility between Java and Matlab2014a(which is my version) here. What should I check in order to correctly link Matlab to Java? A few days ago Matlab was working correctly with Java.
In my PATH I noticed the existence of the following path: C:\ProgramData\Oracle\Java\javapath in which there are three wrong shortcuts to java.exe, javac.exe, and javax.exe. When I type in matlab console getenv JAVA_HOME I am getting C:\Program Files\Java\jdk1.7.0_11 while when I wrote version -java I got: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode. When I write javac in matlab console I am getting: Undefined function or variable 'javac'.
When I try to built C++ library I got a similar issue: Error: An error occurred while shelling out to mbuild (error code = -1).
Unable to build executable.
EDIT: Does the absence of the proper MCR counts in my case?

You said
getenv JAVA_HOME I am getting C:\Program Files\Java\jdk1.7.0_11 while when I wrote version -java I got: Java 1.7.0_11-b21
try to use setenv in MATLAB, doc here http://www.mathworks.com/help/matlab/ref/setenv.html
setenv('JAVA_HOME','C:\Program Files\Java\jdk1.7.0_71');
setenv('PATH','C:\Program Files\Java\jdk1.7.0_71\bin');

Following from the answers in this link
http://www.mathworks.com/matlabcentral/answers/131639-matlab-deploytool-not-working-fails-while-building
seems like you have to install JDK 1.7.0_11-b21, which is the one your MATLAB version is compatible with for compiling into jar. Also, after installing that specific version, you should modify the JAVA_HOME and PATH environment variables. Try that to see if it helps.

I was having a very similar problem with my installation.
I set the environment variable (press > type env > Enter) JAVA_HOME (under "User variables") to my JDK home directory (not the bin folder) without any trailing \ or ;.
The problem occurs because MATLAB cannot execute javac properly, however, in order to work out exactly what MATLAB is trying to execute (which is not working) we need to use the command window and not the Library Compiler.
It appears you have already done this but for completeness I will add the process here for others.
To find what MATLAB executes, click "Open log file" and copy the entire first line.
Paste the first line in the MATLAB command window and add -v to the end.
Press enter to run the command and note the "Executing command:" that is printed to the screen.
Once you have the "executing command" that MATLAB returns for you, check the first path in that command and make sure it matches your javac file.
In your original post you have done this and posted
C:\Program Files\Java\Java\jdk1.7.0_11\bin\javac
a path which contains \Java\Java, if this isn't a typo then it may be a problem as it is not the default Java installation path. You also say after that your installation is at
...\Java\jdk1.7.0_71\bin\bin\javac
which contains \bin\bin and is also not the default java bin install directory, perhaps another typo?
If the path to javac matches exactly you could try copying and pasting the "executing command" into your OS's command window (+R > cmd > Enter) and looking for errors. By doing this we can remove ourselves from the MATLAB installation and if errors still exist it likely has nothing to do with MATLAB.
To see if the same problem could be caused by other environment variables I removed every other path to a java installation (keeping JAVA_HOME) in my environment variables. MATLAB uses the absolute path to the java installation and so it was still able to compile with javac using only the JAVA_HOME environment variable.

Related

javac is not recognized as internal command [duplicate]

This question already has answers here:
javac is not recognized as an internal or external command in windows 7
(3 answers)
Closed 2 years ago.
My current system config is -
Netbeans 12.0
JDK 14.0.2
Windows 10 (64bit)
I have added the environmental variable in PATH manually i.e. C:\Program Files\Java\jdk-14.0.2\bin.
I tried adding using cmd as well. As well as adding the JAVA_HOME as a new variable with value as C:\Program Files\Java\jdk-14.0.2 .
I have used the set command to do this process i.e. set path = "%path%;"C:\Program Files\Java\jdk-14.0.2\bin". Also, I have used the same command without the set keyword.
Since javah command was deprecated for versions of jdk>8, I have tried to use javac -h command as well.
I have also tried to install JDK v. 8.0.2 instead of the current one.
Although there's no javah.exe file in the bin folder, there is however javac.exe file.
I have uninstalled and reinstalled JDK and Netbeans quite a few times.
I am currently working on JNI with Netbeans IDE and C/C++ plugin. (I have successfully installed Cygwin and necessary extension for the file to build. Although I don't know how they affect this problem, still I thought I should add this).
The command I wish to use with 'javah' is the one used for creating a C header from a Java class i.e. 'javah -o JNIDemoJava.h -classpath JNIDemoJava/build/classes jnidemojava.Main'.
But due to this error, I have been stuck for a few days. Please do assist.
Following way you can set Path variable temporary(it can only be used in the same command line):
You should do it this way:(notice =)
set PATH=C:\Program Files\Java\jdk-14.0.2\bin
And if you set JAVA_HOME variable correctly you can do it this way:
set PATH="%JAVA_HOME%\bin"
For setting this permanently read this post :
https://javatutorial.net/set-java-home-windows-10
We should know the reason for this
Our OS comes with a predefined (built-in)set of tools and utilities. When we try to execute the command e.g. cls in the Windows command line then it is already present in system path variable and os will refer the corresponding binary of cls to execute the command.
However, when we install any third party tool/software then path variable is not updated accordingly.
When we install different versions of java on your system then installations go to different directories. E.g. JDK installation directory for Windows will be
C:\Program Files\Java\jdk1.8.0_161
Similarly, JRE installation directory for Windows will be JDK installation directory for Windows will be
C:\Program Files\Java\jre1.8.0_161
We need to update the path variable of OS to point to the appropriate directory. If we set the path of JDK then it will execute a binary from JDK bin directory.
Solution
we need to update JDK or JRE version specific directory location into PATH Environment variable.
In this case use
set path=C:\Program Files\Java\jdk-14.0.2\bin to set JAVA_PATH

JAVA_HOME is not defined correctly on Ubuntu when trying to run Ballerina code?

I have already set JAVA_HOME in the environment variables file as shown below.
I also built ballerina from the source and extracted the zip with the Runtime and the tools as per the Ballerina documentation. However when I try to give this distribution as the Ballerina SDK in IntelliJ IDEA or even in the Terminal and try to run a Ballerina program as shown below:
I get the following error:
Error: JAVA_HOME is not defined correctly.
But when I type:
echo $JAVA_HOME
I get the following output:
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
What am I doing wrong?
Note: Ballerina Version: jballerina-tools-2.0.0-Preview3-SNAPSHOT
The value of your JAVA_HOME environment variable is wrong. It should not to point to an executable but to the location (i.e. a directory) of the Java installation.
In your case the correct value seems to be:
/usr/lib/jvm/java-8-openjdk-amd64
See also e.g. How to set JAVA_HOME for Java?.
Normal Ballerina 1.2 distribution doesn't require JAVA_HOME but this might be different in your case as you're building a preview release.
what #user272735 was partly correct but you can see that I have added the correct Java Home path in the environment file. Actually the problem was that I was working on terminator when changing the java home path in the environment file and running the below command.
source /etc/environment
But I was trying to run the ballerina code in the plain-old Terminal where the path change was not registered yet.
How to resolve:
I had to run the below command in the Terminal as well. Then when I echoed the java path it printed the one in the environment file and the ballerina code also ran without a problem.
source /etc/environment

Java and javac not working in cmd. Both java and javac "is not recognized as an internal.."

I am having som trouble with java, i have tried to fix my Path, but i am new to programming and don`t know what to do.
I have java installed at : C:\Program Files\Java\jdk1.8.0_92\bin
and at : C:\Program Files (x86)\Java\jdk1.8.0_91\bin
Here are some screenshots from my system variables: This pops up when i click on the system variable "Path", see the other picture.
System variables
When I am in cmd, and writing java -version or javac -version, I get this message:
"java/javac is not recognized as an internal or external command, operable program or batch file".
Have tried to restart cmd every time i have changed the path.
Try Setting separate variables for JDK & JRE
Also give a backslash "\" after bin in the case of both JDK & JRE
Hope this works
First go to this path to make sure that it is right.
second try to restart your cmd and try again.

javac not working in CMD

I'm having a issue with compiling java from my Command Prompt. At first it was saying javac isn't a recognised internal or external command and by reading up I see you need to change the path and thats what I did exactly to...
variable name: path
variable value:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%path%;C:\Program Files\Java\jre7\bin;
I closed commpand prompt and attempted it again however the same issue came up.
I then tried echo %path% and the entire path line was echoed(entire line in terms of that variable value)
I also have tried "for %i in (javac.exe) do #echo %~$PATH:i" and that returns ECHO ON.
I'm quite stumped and confused now. My javafile is in and named main.java however I am trying to compile it whilst in the javawork folder in CMD
C:\Users\MYNAME\Documents\Javawork
Thanks,
You want the path to the JDK, not the JRE, in your system path. The JRE doesn't come with a compiler (javac).
To compile Java code you need to install the Java Development Kit (JDK) as javac.exe isn't included in the Java Runtime Environment (JRE).
The path should point to the bindirectory of the JDK.

JAVA_HOME error in Titanium Studio on Linux Mint

I will try and keep this as concise as possible since I need to get this figured out quickly. Back when I setup Titanium Studio 2.1.1.201207271312 on Linux Mint 13 64-bit system, I had all kinds of problems installing a version of Java Titanium liked. In the end I Think I used the instructions here to get it working.
Now all these months later when I try to compile an app for Google Play I get this dreaded error:
[ERROR] Program launch failed. Unable to locate Java VM. Please set JAVA_HOMenvironment variable.
[ERROR] Unabled to prepare JavaScript for packaging. Error code 4.
Checking the java alteranatives on my system I get this:
'update-alternatives --query java
Link: java
Status: auto
Best: /usr/lib/jvm/java-6-sun/jre/bin/java
Value: /usr/lib/jvm/java-6-sun/jre/bin/java
Alternative: /usr/lib/jvm/java-6-sun/jre/bin/java
Priority: 63
Slaves:
java.1.gz /usr/lib/jvm/java-6-sun/jre/man/man1/java.1.gz
Alternative: /usr/lib/jvm/java-7-oracle/bin/java
Priority: 1
Slaves:
java.1.gz /usr/lib/jvm/java-7-oracle/man/man1/java.1.gz
I have the following set in my .profile
export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre/bin/java
export PATH=$PATH:/usr/lib/jvm/java-6-sun/jre/bin/java
But still I get the error in Titanium. I have tried numerous different variations of my .profile file, but with no luck.
I really need to work this out quickly. Does anyone have any ideas?
EDIT: Solved
I seem to have worked the proper paths, here is how my .profile now reads:
JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
export JAVA_HOME
PATH=$PATH:/usr/lib/jvm/java-6-sun
export PATH
.profile is usually processed by sh, while .bash_profile is processed by bash (and instead of .profile when present). Try following sh compatible format (which has bitten me before when mucking with .profile files):
JAVA_HOME=/usr/lib/jvm/java-6-sun
export JAVA_HOME
PATH=$PATH:/usr/lib/jvm/java-6-sun/bin:/usr/lib/jvm/java-6-sun/jre/bin
export PATH
I Corrected to what I usually use, I was just echoing your values, but I should have been more careful. I usually have JDK and JAVA_HOME is supposed to be the base for it and then I add bin for both JDK and JRE. If you compiling then you may need the JDK and not just the JRE.
I seem to have worked out the proper paths, here is how my .profile now reads:
JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
export JAVA_HOME
PATH=$PATH:/usr/lib/jvm/java-6-sun
export PATH

Categories

Resources