Matlab and Java integration - java

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

Related

The error I get Which checking Apache Pig Version "Error: Could not find or load main class C:\java\lib\tools.jar"

I have my java working properly and also the environment variable is also set for java jdk 1.8.0_261
But when I run the pig -version its shows Error: Could not find or load main class C:\java\lib\tools.jar.
I have also checked the java folder in lib for tools.jar and it is there.
Also the Apache Pig configuration is also correct in pig.cmd
My Hadoop and MapReduce all are working fine with no error.
Your installation is broken, or you have messed up a shell script. That error cannot really mean anything else.
When invoking java.exe, you can either pass a jar file, via java -jar C:\foo.jar, or a classpath + fully qualified class name: java -cp C:\foo.jar;C:\bar.jar com.foo.fullyqualified.ClassName.
That error means you've passed C:\java\lib\tools.jar in the position of that classname. That will fail to run on any system and regardless of installations: C:\java\lib\tools.jar is a file path, not a fully qualified class name. That tools.jar exists in that location is immaterial: It's as if I told you: "I searched for the book titled "Can of Paint" on your bookshelf and I can't find it", and you going: "That's bizarre, because (pointing at the cans of paint on the shelf in the garage), there are cans of paint right here". Java isn't treating that as a path to look up; it is treating it as a class name to search for on the classpath.
Note that this trivial invocation:
java.exe C:\lib\tools.jar
produces the exact error you are observing and that is the correct behaviour of java (because the right way to run it is either java -jar C:\lib\tools.jar or more likely java -cp C:\lib\tools.jar;. com.foo.YourApp. The fix is to.. not invoke java incorrectly like this.
Reasons
Perhaps pig.cmd is broken. The image you pasted does not include the actual call to java.
Perhaps java is broken. Does java.exe work at all? Make some test file, compile it, run it. Does that work?
Perhaps the script is trying to start the jar by letting windows do it (so, double clicking it, or in a script, start foo.jar, which will then end up checking the HKLM registry section for .jar, and then checking the key there for how to start it, and if you've manually edited that, that would break things: You then presumably set it for e.g. java %1 which is broken (it'd have to be java -jar %1).
pig -version doesn't invoke the pig.cmd you think it invokes, but something else.
Something in pig.cmd starts another script. Then that script is broken, or isnt the script pig.cmd thought it was, etc.

IntelliJ: cannot launch a java application using the command line

Let me summarize the problem:
An exercise asked me to launch a java application using the Windows 10 Command Line.
It taught me on a video how to add the IntelliJ path to the Environment Variables, so that, whenever I typed java Main I would get my Main.java/class application back.
The first problem that I had stumbled upon was that, instead of using java Main, I'd have to type java Main.java. I thought that it was due to a recent build for the JDK (v14.02), so I let that one pass.
The second problem that I had encountered was that, when the exercise asked me to launch an application using the package directories, the command line would return the following error: Error: Could not find or load main class com.pluralsight.organized.Main. Caused by: java.lang.ClassNotFoundException: com.pluralsight.organized.Main.
Revert whatever damage you've done to the system.
Install the Oracle JDK.
Put the Oracle JDK bin folder on your PATH variable.
Compile your code using javac Main.java
Run your compiled code using java Main
for the ones in the packages, you need to be at the package root, or have a class path to it. For example, let's consider a class com.example.Main
The folder structure should be:
D:/SomePath/com/example/Main.class
Then you need to be at D:/SomePath/ to execute java com.example.Main.
Alternatively, you could be anywhere else and execute java -cp D:/SomePath com.example.Main.

MATLAB code conversion to 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.

Making a "macro" command to run a program

I have a Main.java file and I want to run the program passing it test.txt
I know in command line I can write javac Main.java
After compiling I can write java Main test.txt and this will accomplish running the file and passing test.txt
If I wanted instead to be able to just write main test.txt and have that trigger my Main.class file to run is that possible and if so how?
(Edit: Based on your comment, let me expand to add a couple more situations)
If your goal is to have someone else run your program who does not have Java installed, and you do not wish to have them install a Java runtime environment before running your app, what you need is a program that converts the .class or .jar files into a native executable for the platform you are using. How to do this has been covered in other questions, eg: Compiling a java program into an executable . Essentially, you use a program like JCG (GNU Compiler for Java) or Excelsior JET (a commercial product) to expand the byte code into full native code with a mini-JRE built in.
If your goal is to save typing, there are a number of strategies. Others have suggested alias commands, which work well on linux.
A slightly more portable option that you could ship with your program would be a shell script. Granted, shell scripts only run on linux or other OS's with shell script interpreters installed.
Here is an example shell script. You paste this into a text editor and save it as main with no extensio. The $1 passes the parameter argument fyi.
#!/bin/sh
java Main $1
presuming you name your shell script just "main" with no extension, you could call main test.txt to execute your program now.
If you are on Windows, you might want to create a windows shortcut, and point the shortcut to "java Main test.text", using the full paths if necessary (if the paths are not already set). Of course, this does not make the parameter easy to change every time you run it, you would have to edit the shortcut.
add an alias
e.g. under a mac edit your .bash_profile with the following line
alias main='java main'
don't forget to open a new console to see your alias working
Depends on your operating system. On Linux with the bash shell, for instance, you can set up an alias to expand your main into java -cp myjar.jar main.
Linux can also be configured to 'understand' Java class flies as a binary format directly see here (linux kernel documentation).
If you're on windows, you'll have to wait for answer from someone with more knowledge about that than I.
Good luck!

Runtime exec output path

I am trying to run a perl command with Java runtime exec in linux/ubuntu/gnome. The command generates an pdf file, but it saves it in my home folder. Is there any way that the exec method can set an output path for the commands executed? Thanks in advance.
The exec method just runs the command on the operating system, so you'll want to change the command you're running with exec more than anything in "Java" per se.
There are a few possibilities:
Change the working directory of your java program. The .pdf is being saved in your working directory because this is where the program is being run.
Unfortunately it's not simple to change this value after the program has been launched. It is, however, trivial to change before the program starts; just change the working directory before starting the program.
Move the file to it's desired location after it's been created in your home directory.
Change the command so that it includes the target location. Your perl script may have an option that will enable you to save it's output to a certain location (usually -o or --output). Using this your program would change from:
Runtime.exec("perl someprogram");
to something like:
Runtime.exec("perl someprogram -o /path/to/some.file")
You might be able to use "output redirection", if there is no option to do this.
Try something like what's below as your argument:
Runtime.exec("perl someprogram > /path/to/some.file")
Unfortunately, without knowing more details of your situation I can't provide more concrete advice.
While each approach has benefits and drawbacks, it's probably best to just implement the one that you understand best; if you can't get one to work, try another.
A good, free online resource for learning is Introduction to Linux: A Hands On Guide.
Section 2.2 has details on cd which you can use for 1..
Section 3.3, section 3 teaches about the mv command, which will be useful in 2..
Section 5.1 is about I/O redirection. Knowing about "output redirection" and the > operator, are important for 4..
For 3., you'll have to consult the documentation of the perl program you're using.
You could modify the Perl script to accept an absolute path for the output.
You can trying setting the working directory using exec(java.lang.String[], java.lang.String[], java.io.File) where File is the directory the command is executed from.
If all else fails, you'll can always copy the generated file from the Home directory to your final location.

Categories

Resources