MATLAB code conversion to Java - java

I am trying to convert this code into Java Package using the MATLAB compiler and I'm getting this error on building. Can someone suggest anything?
'javac' is not recognized as an internal or external command,
operable program or batch file.
Error: An error occurred while shelling out to javac (error code = 1).
Unable to build executable.
Is there any specific command line to be included into the matlab function file (for example we include %#codegen for C/C++ conversion)

Make sure that you have the Java compiler installed. Try executing java -version and javac -version at a system prompt, and ensure that these both run and give a version that is supported by your version of MATLAB.
Check also that your system environment variables are set appropriately. In particular the JAVA_HOME variable should be set to your JDK path.

Related

Getting an error while running uiautomatorviewer in ubuntu 20.04 [duplicate]

This question already has answers here:
uiautomatorviewer - Error: Could not create the Java Virtual Machine
(11 answers)
Closed 1 year ago.
The following error is displayed when the command is executed:
username#device:~/Android/Sdk/tools/bin$ ./uiautomatorviewer
Djava.ext.dirs=/home/username/Android/Sdk/tools/lib/x86_64:/home/username/Android/Sdk/tools/lib is not supported. Use -classpath instead.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
then I use --class-path in uiautomatorviewer file and face this error:
I use this method for editing uiautomatorviewer file: https://www.programmersought.com/article/53371586152/
error: unable to initialize main class com.android.uiautomator.uiautomatorviewer caused by: java.lang.noclassdeffounderror: org/eclipse/swt/widgets/control
mychange:enter image description here
original:enter image description here
And when I got the above error, I turned back to the previous state
And now I have the first issue again.
extera information:
$JAVA_HOME => /usr/lib/jvm/java-8-openjdk-amd64
$ANDROID_HOME => /home/username/Android/Sdk
Android studio version: 4.1.3
This is the output that I asked for in the comments.
+java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)
+exec java -Xmx1600M -Djava.ext.dirs=/home/alielyasi/Android/Sdk/tools/lib/x86_64:/home/alielyasi/Android/Sdk/tools/lib -Dcom.android.uiautomator.bindir=/home/alielyasi/Android/Sdk/tools -jar /home/alielyasi/Android/Sdk/tools/lib/uiautomatorviewer-26.0.0-dev.jar
-Djava.ext.dirs=/home/alielyasi/Android/Sdk/tools/lib/x86_64:/home/alielyasi/Android/Sdk/tools/lib is not supported.
Use -classpath
Firstly, we can see that the Java command that is being used is java, not a specific (absolute) pathname.
That is OK, but it means that $PATH will be used to resolve java to the actaul pathname of a Java launcher. (Not $JAVA_HOME.)
Second, we can see that java -version is telling us that it is using Java 11.0.11. As I mentioned in my comments, Java 9 and later do not support the "ext.dirs" mechanism.
Unfortunately, the java command's suggestion to use -classpath is not going to work (as-is).
So I am going to give you two ways to solve this:
Solution #1: Just use Java 8.
You need to make sure that you have Java 8 installed, and that running the java -version prints out that that the version number is 8.0.xxx, where xxx is (ideally) the latest available Java 8 patch release.
Since you are using using Ubuntu 20.04, there are two ways to make sure that you are using Java 8 rather than Java 11.
You could make Java 8 the default Java version globally using the "alternatives" system. Run man update-alternatives for the documentation. (You need to run update-alternatives as root ...)
You could make Java 8 the default for the current user or the current shell by changing the PATH environment variable.
Solution #2: Adjust the script so that Java 11 will work.
As I said above, simply converting -Djava.ext.dirs=... into a -classpath option doesn't work. That is because the exec line is using the -jar option, and when -jar is used -classpath is ignored. But -jar means this is an "executable JAR", and the entrypoint class name is coming from the JAR file's manifest.
So we need to unpick this.
First we need to know what the (full) entrypoint class name is, and also JAR file is setting its own classpath. We can determine by using the jar command to extract the META-INF/MANIFEST.MF file and looking it.
Next we need to form a classpath consisting of
the path that is currently given by $frameworkdir
the path for the main JAR file (i.e. $jarpath)
the path specified in the manifest file
The order of the path could be significant.
The entire classpath needs to be given to the java command via a -classpath or -cp option. (Not --class-path.)
Finally remove -jar $jarpath and replace it with the full entrypoint class name. (It is a class name, not a pathname. Don't change the dots to slashes, add a suffix or do anything like that. Use the name exactly as the manifest file gives it.)
This approach should work, but I can't test it. If it fails, please comment below.

Setting up Sublime Text up for Java and getting "bash: javac: command not found"

I am having trouble in setting up Sublime Text for Java programming.
When using the JavaC Build System, I am getting an error like this in the terminal:
bash: javac: command not found
[Finished in 0.1s with exit code 127]
[shell_cmd: javac “/home/vinays/Documents/HelloWorld.java”]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]
Then I had made myself a new build system like this:
}
shell_cmd”: “java $file_name”,
“working_dir”: “${project_path:${folder}}”,
“path”: “/usr/bin/java”
}
Again it went wrong and shows:
/usr/bin/env: ‘bash’: No such file or directory
[Finished in 0.0s with exit code 127]
[shell_cmd: java HelloWorld.java]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]
You've got a lot going wrong here, so lets take it in stages.
“bash: javac: command not found
[Finished in 0.1s with exit code 127]
[shell_cmd: javac “/home/vinays/Documents/HelloWorld.java”]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]”
This is an indication that the sublime-build file you tried to use (perhaps the one that ships with Sublime) tried to invoke javac, but it was not found anywhere on the path.
You also included the following in your answer:
~$ which java
/usr/bin/java
This is an indication that java is indeed available on the path; this is the command that you use to execute a Java program.
Since you have java but do not seem to have javac, my guess would be that you installed a JRE (Java Runtime Environment) and not a JDK (Java Development Kit). The first is meant for running Java applications only, while the second actually contains the tools and additional files needed to compile Java code.
So, your best bet is to verify that you actually have a JDK installed (perhaps by executing which javac to see what it says).
Further to that, you also provided this sublime-build file:
{
"shell_cmd": "java $file_name",
"working_dir": "${project_path:${folder}}",
"path": "/usr/bin/java"
}
This is wrong for a couple of reasons. First, it's using java and not javac, so even if it did work and launch java, it would fail because java is for executing the compiled class file, not for compiling your code.
Secondly, path in a sublime-build file does not do what you think it does. Specifically, it tells Sublime to completely erase the contents of your PATH environment variable and then replace it with the content that you provide.
The PATH is for specifying the list of directories in which programs you want to run interactively live; here you've set it to the actual physical location of the java program itself (i.e. not a folder).
With that change in place, nothing can run any longer because the PATH is entirely invalidated. Hence your error here:
/usr/bin/env: ‘bash’: No such file or directory
[Finished in 0.0s with exit code 127]
[shell_cmd: java HelloWorld.java]
[dir: /home/vinays/Documents]
[path: /app/utils/bin:/app/sublime_merge/bin:/app/bin:/usr/bin]
This is the OS telling you that Sublime tried to run bash (the default shell), but it could not be found. It can't be found because the path was broken by your build (the path: listed is gathered for the debug diagnostic before the build starts, so it represents what the path was prior to the sublime-build file breaking it).
It's also worth noting that a command like javac Something.java just compiles the Java code, and then stops; this is almost certainly not what you want to do. You probably want to actually execute the code as well.
So, after you ensure that you actually have both java and javac, you should be able to get yourself up and running with a sublime-build file like this:
{
"shell_cmd": "javac \"$file_name\" && java \"$file_base_name\"",
"working_dir": "${project_path:${folder}}",
"selector": "source.java"
}
This will compile the single Java file and then, if it worked, execute it. Note that because your other build broke the PATH, you need to restart Sublime before any build will work.
This also doesn't help you work with any Java program that's more complicated than a single file. Java as a language does not lend itself well to ad-hoc compiling and running of files because the class files need to be in the appropriate package-specific location.
At some point you will need to upgrade yourself to using some Java-aware external build tool from Sublime, such as Ant or the like.
Yes Sir I have verified my system commands i had got the following result:
~$ which java
/usr/bin/java
~$ which javac
/usr/bin/javac
More Over My path in sublime text is also Like This:
{
"shell_cmd": "java $file_name",
"working_dir": "${project_path:${folder}}",
"path": "/usr/bin/java"
}

Correctly install Java for Matlab

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.

Matlab and Java integration

I have done image processing in MATLAB and build my GUI in Java. I want to integrate MATLAB into Java. I want to use MATLAB Builder for this purpose. I want also to use neural network for classification. There are some excel files also. Is it possible that this code will be integrated in Java?
My other question is that I want used MATLAB BuilderJA to know how it works. When I type java -version command, it gave me this error.
??? Attempt to execute SCRIPT java as a function:
C:\Program Files\MATLAB\R2009b\toolbox\matlab\general\java.m
C:\Program Files\Java\jdk1.6.0_21
When I use build command it gave me this error.
'javac' is not recognized as an internal or external command,
operable program or batch file.
Error: An error occurred while shelling out to javac (error code = 1).
Unable to build executable.
I have JDK installed. The path is C:\Program Files\Java\jdk1.6.0_21. I am using R2009b version
I want to run my code in Java, but I do not know how to fix this error. Can any one tell me how to resolve this error?
The first error message which you get when you type java -version is a bit misleading; instead of
java -version
you need to say
!java -version
since you want to call an external program and not a MATLAB script or function. As stated in the comments by Amro this will only work if the directory containing java.exe is on your path. See Running External Programs in the MATLAB help for more info.
The error message you get comes from the fact that
there happens to be a file java.m and MATLAB thinks you are trying to call this file
that file only contains comments, since java is actually a sort of keyword in MATLAB, see doc java.
MATLAB realizes that you are not using the keyword in its correct form (which would be to call java.something to create an object of class something) since you give a parameter
MATLAB ends up telling you in a strange way that java doesn't accept parameters (even though java.m does not contain the script, only its documentation)
Note that if you don't want to add the directory containing java.exe and javac.exe to the path, you could also try calling them with their full path name:
!C:\Program Files\Java\jdk1.6.0_21\blablabla\bin\javac.exe

matlab deploytool to java package javac error

I'm trying to wrap a program of mine to work with java.
I tried a simple "hello world" first,
-hello world.m-
disp('hello world');
I used deploytool and selected java package.
when it reached this line:
Executing command: "javac -verbose -classpath "C:\Program Files\MATLAB\R2009b\toolbox\javabuilder\jar\javabuilder.jar" -d "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\classes" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\helloworld.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\DeployTutorial2MCRFactory.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\helloworldRemote.java" "C:\Users\shachar\Documents\MATLAB\deployTutorial\deployTutorial2\src\deployTutorial2\package-info.java""
I got this error:
'javac' is not recognized as an internal or external command,
operable program or batch file.
Error: An error occurred while shelling out to javac (error code = 1).
Unable to build executable.
btw: when I tried standalone application / c/c++ shared library it has been compiled successfully.
thanks in advance
Possibly the Java SDK is not installed or properly configured on your machine. Open a system terminal and execute the following two commands:
java -version
javac -version
If they both work you should proceed with the examples from the MATLAB help. If not install the Java SDK.
First you should install JAVA.
Then you must set the environment variable in "my computer"
Add a new variable named "JAVA_HOME" and set its value to your jdk path
like D:\Program\Java\jdk1.6.0_25
Then restart your matlab
and type
getenv JAVA_HOME
you should get
ans=
D:\Program\Java\jdk1.6.0_25

Categories

Resources