11

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?

Jose Ramon
  • 5,572
  • 25
  • 76
  • 152
  • are you using Windows 7 64 bit? – ale64bit Jan 13 '15 at 13:31
  • Yes, and I have install jdk-7u71-windows-x64 version of java. – Jose Ramon Jan 13 '15 at 13:32
  • what's the output of `version -java` from MATLAB command window? – ale64bit Jan 13 '15 at 13:38
  • Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode. Hmm however I cant locate it in my path. – Jose Ramon Jan 13 '15 at 14:08
  • Have you checked that there is permission to write to the directory in which the output would be generated? – Jool Jan 18 '15 at 12:33
  • Possibly related: [Link1](http://www.mathworks.com/matlabcentral/newsreader/view_thread/316330) and [Link2](http://stackoverflow.com/questions/6842620/matlab-and-java-integration) --Personally I would consider to uninstall matlab and java, and then install matlab. It should automatically include the correct version of Java. If that still not works it seems like it is time to contact mathworks support. – Dennis Jaheruddin Jan 20 '15 at 14:21
  • Jool the weird is that before two weeks I had created the jar file. Something changed in my system and then I couldnt build jar. – Jose Ramon Jan 21 '15 at 08:08
  • In the first link it is proposed classpath to be : CLASSPATH : .;C:\Program Files\Java\jre7\lib\ext\QTJava.zip;. However I couldn't locate such a file in my system. – Jose Ramon Jan 21 '15 at 08:43
  • I have unistalled java and Matlab. Afterthat, I ve installed matlab 2014b. Command version -java in matlab console give me: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode. What should I do next, java isnt installed in my computer, which version should I install? – Jose Ramon Jan 21 '15 at 10:36
  • @JoseRamon Perhaps I misundestand it fundamentally, but as shown from the version output, you do seem to have java. No doubt you also can do stuf like `java.lang.Thread.sleep(3000)` to pause your code for 3 seconds. If that happens and you still get the same error I think you should contact mathworks support. Usually de respond quite well. – Dennis Jaheruddin Jan 21 '15 at 16:54
  • java.lang.Thread.sleep(3000) working fine. However now I am facing a new problems after re-installing matlab . When I tried to build java package I am received the following message: Test checkout of feature 'Compiler' failed. mcc failed. – Jose Ramon Jan 22 '15 at 08:20
  • do you have a license for the compiler that was applied to your re-installation? http://www.mathworks.com/matlabcentral/answers/121413-error-using-mcc-test-checkout-of-feature-compiler-failed what is the result for license('checkout','Compiler') – brettmichaelgreen Jan 26 '15 at 22:11
  • license('checkout','Compiler') returns 1 – Jose Ramon Jan 27 '15 at 08:07

3 Answers3

2

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');
  • My environment variables are already correct. I tried with your approach, however I am getting the same error. Error: An error occurred while shelling out to javac (error code = -1). Unable to build executable. When I use getenv I got jdk1.7.0_11 both for path and java_home. – Jose Ramon Jan 26 '15 at 09:04
  • I am guessing that there is a problem with a java.m file, I cant think other problem here. – Jose Ramon Jan 26 '15 at 09:31
1

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.

ale64bit
  • 6,232
  • 3
  • 24
  • 44
  • The weird is that before one week, I was able to build jar files normally. – Jose Ramon Jan 13 '15 at 14:13
  • and I guess you never went through some update of the JDK? What else changed in your system related to MATLAB or Java? – ale64bit Jan 13 '15 at 14:14
  • Windows updates the only thing. However, I didnt notice what was the change. – Jose Ramon Jan 13 '15 at 14:16
  • Well, then I would suggest you to install the JDK that MATLAB mentions, just to try. Also change the env variables and restart MATLAB after it. – ale64bit Jan 13 '15 at 14:17
  • missing javac? but you downloaded JDK or JRE? – ale64bit Jan 13 '15 at 14:40
  • I have already deleted my comment. Firstly I downloaded just jre. Afterthtat I downloaded JDK, add it to JAVA_HOME however I got the same error when tried to build jar file from matlab. I have downloaded jdk-7u11 from here http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html#jdk-7u11-oth-JPR – Jose Ramon Jan 13 '15 at 14:47
  • 1
    First, get sure you updated JAVA_HOME and PATH with the correct values. Then restart MATLAB. Then, tell me the output of `getenv JAVA_HOME` from MATLAB command window. – ale64bit Jan 13 '15 at 14:50
  • The output of getenv is the proper C:\Program Files\Java\jdk1.7.0_11. The error that I got in matlab log file is Error: An error occurred while shelling out to javac (error code = -1). Unable to build executable. Executing command: ""C:\Program Files\Java\jdk1.7.0_11\bin\javac" – Jose Ramon Jan 13 '15 at 14:53
  • The console output for javac java -version is: java(c) 1.7.0_11 – Jose Ramon Jan 13 '15 at 14:56
  • 1
    And why ""C:\Progra... starts with double quotes? – ale64bit Jan 13 '15 at 14:58
  • I have no clue about that. – Jose Ramon Jan 13 '15 at 14:59
  • In environment path in PATH I have 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). – Jose Ramon Jan 19 '15 at 10:21
  • mmm.....what do you mean by "wrong shortcuts"? did you try to remove that entry from PATH already I guess? – ale64bit Jan 19 '15 at 14:15
  • Basically when I double click it I am getting path does not exist. – Jose Ramon Jan 19 '15 at 14:18
  • It points to C:\Program Files\Java\jre1.8.0_20\bin. Yes, had the same errors. When I try to write javac from matlab console I am getting Undefined function or variable 'javac'. Is this normal? – Jose Ramon Jan 19 '15 at 14:19
  • Then try to remove that entry from PATH, then erase those shortcuts just in case (they seem like a trace of old JRE8 install), then restart MATLAB and try again. I don't really know what can it be already. Remember that when you change the PATH, you have to restart MATLAB also. – ale64bit Jan 19 '15 at 14:21
  • Yes, it is normal. You don't call javac from inside MATLAB in the command line. – ale64bit Jan 19 '15 at 14:23
  • The same error about *"...while shelling out to javac"* with the SAME error code? – ale64bit Jan 19 '15 at 14:29
  • Yes. Error: An error occurred while shelling out to javac (error code = -1). Unable to build executable. Executing command: ""C:\Program Files\Java\jdk1.7.0_11\bin\javac" – Jose Ramon Jan 19 '15 at 14:31
1

I was having a very similar problem with my installation. I set the environment variable (press enter image description here > 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.

Library Compiler Error

Error Log

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 (enter image description here+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.

JacobD
  • 627
  • 6
  • 15