I need to be able to change the working directory in MATLAB without interacting with the command window. I'm launching MATLAB from a Java application. Right now the only solution I've come up with is closing MATLAB, changing directory from JAVA and relaunching. Is there some streamlined way to send MATLAB the 'cd' command from JAVA? Doing so from the command-line would also work, since I could use getRuntime().exec(command)
Thanks!
You can do this using JMI if you're using the same JVM as Matlab (if not then I have no idea). There is not much online info about this (it's WAY undocumented/unsupported). Google it or read this: http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. In short, you need to include Matlab's relevant JAR file and then use com.mathworks.jmi.Matlab's functionality. For example:
Matlab.evalConsoleOutput("cd('C:\Program Files\')");
Yair Altman
http://UndocumentedMatlab.com
I am working with Stephen Poletto who posted the original question. There wasn't any existing solution that met our needs so we wrote our own solution based off of Kamin Whitehouse's work mentioned by Yair. It is available for all to use at matlabcontrol.googlecode.com
It allows for controlling MATLAB from a Java program launched outside of MATLAB.
Related
I would like to get the path to the working directory of a specific process (for example for the PID of the process). I am Not Talking about the working or current Directory of the process where my Java Code is running. Its a simple task with Linux, but for Windows i cant find a proper solution. Furthermore, it would be nice, if its a Command or a Framework for Java, because i will need the path in my Code. I am not looking for the path to the executable, also Not for a solution with wmic or process explorer.
Already thanks for the help.
I already tried commands like tlist and wmic, but those solutions cant be utilize in my code. I am looking for a solution that i can use without special installations on Windows.
JNI and JNA provide means to call directly into native libraries from Java code, and it is feasible to use these to call out to Windows libraries.
There is a github project that appears to be close to the need: https://github.com/kohsuke/winp. Perhaps you can add the needed code and send up a pull request, or fork the project.
Note that any solution here is going to be windows-specific, meaning the application using it will not run on another platform. Given the nature of the question, that doesn't sound like it would ever be a concern.
I wrote a simple java code that would take simple inputs from the user in the command window (of eclipse for me) using nextInt() and nextLine(). However, I realized that others need JRE (I believe?) on their computer to run the executable jar file made. So I was wondering if there is a way to get around that by making the app produce a window that is like the command window to have the same interaction as the command window in eclipse.
So, if I were to run the .jar or .exe then a simple window would pop up that acts like the console of eclipse displaying lines from System.out.println() and etc.
To run a java program you need the jre. There is no way around that.
If you need the console, nothing is stopping you from running the java program from the windows command line, which will do exactly what you ask for.
You still need the JRE.
Unfortunately, when starting to learn Java with Eclipse, many people miss the opportunity to at least start to understand how to do the same from the command line, which is, if you ask me, good to know.
For programs written in Java, they are compiled as a jar file, like you mentioned, and how these compiled versions of your source code differs from many other programming languages is that they do not contain the assembly/machine code like for example a compiled C program would have. They are instead compiled as bytecode. Which is special code for execution by a Java Virtual Machine. Here is a good Wikipedia reference: link
To answer your question, yes, others need a JRE (Java Runtime Environment) and this can be either:
Installed by themselves (this is what you mentioned)
Packaged together with your java app, to provide a download-and-click experience.
For option 1, assuming they already have it installed, they can simply run it by executing the jar file with javaw, more information on that is in this previously answered SO question
For option 2, the process is fairly lengthy and I'll point you to the official docs to refer to: self contained executables and Deploying java apps
If you have a more complex project with third party libraries and what not, look at this SO question
In the past, I've also found launch4j, a cross-platform wrapper to be very useful, it automates the process of going from jar to an executable (made a simple game that using Swing, simple and ugly thing it was), but the user still needs a JRE, nonetheless. :)
Hope this helps!
I'm reading pdf files with python. And in my script, i'm calling a jar file by command line using os.system.
Two things here:
OSX annoyingly keeps redirecting me to desktop everytime the jar is called. I can optmize that, but I don't want OSX to do that; and
Isn't there a way to no actually open java internally ? Like attaching the code inside my python script. Let's just say I don't want the user to see java being opened.
Thanks in advance.
You may want take a look at similar discussion here. To summarize the discussion, you can use Py4J.
There is also another project called JPype, however that seems to be a very old implementation (way back in 2009) and no updates thence.
This question already has answers here:
how to change the name of a Java application process?
(10 answers)
Closed 8 years ago.
If a Java program is started, it get's in the system process-monitor the name java. Many Java-programs are that way hard to distinguish. So it would be nice, if a way exists, to set the name, that will be shown in the process-monitor. I'm aware that this may work different on different Operating Systems.
A simple way would be, if the java-interpreter would support a switch to set the name, like this:
java -processname MyProgram -jar MyProgram
But I couldn't find such a switch, so it is probably non-existant. An API in Java to set the process-name would be also fine.
So, so you have any suggestions?
I don't know if this is possible, but you could use a command line tool that comes with the JDK called 'jps'. It's like *nix ps, but just Java programs instead. jps -v shows all the arguments you have passed to java.
Also, I have seen people attach a "process name" to their java processes by adding an unused -Dmyprocessname to the args.
as #omerkudat said:
jps -v
prints out all java processes {processID, params list}
If the params list is not enough to recognize the applications you need,
try adding some dummy params when running them:
java -Dname=myApp -cp myApp.jar some.client.main.MainFrame
This will print like:
7780 MainFrame -Dname=myApp
and you can use the process ID to kill / monitor it.
You can do this with an LD_PRELOAD shim: https://github.com/airlift/procname
The shim simply calls the Linux-specific prctl() when the process starts:
static void __attribute__ ((constructor)) procname_init()
{
prctl(PR_SET_NAME, "myname");
}
The call has to happen on the main thread, so it isn't possible to do this from Java or even with a JVMTI agent, since those happen on a different thread.
When I first read this, the idea of changing the process name struck me as impossible. However, according to this ancient thread on the sun forum you can use C++ wrappers around the JVM executable to achieve this.
Though frankly, I wonder what your real problem is, as I'd guess there is a more standard solution then attempting to change the process name.
Your best option is something like launch4j
http://launch4j.sourceforge.net/
There is a bug logged in the sun bugtracker for this, but it's not high priority
http://bugs.sun.com/view_bug.do?bug_id=6299778
There are mainly 2 approaches: one is as already described: using tools like Launch4j, WinRun4J to create native Windows launchers.
Another approach that seems better is to use Apache Procrun to wrap the java application as a Windows service. During the install service process, we can give the process an meaningful name such as OurApp.exe.
All we need do is rename prunsrv.exe to OurApp.exe and replace every occurrence of prunsrv.exe in our install|start|stop|uninstall service scripts to MyApp.exe.
See more from Using Apache Procrun to Rename Process Name of a Java Program in Windows
If you want to use a different process name you'll have to create your own binary to launch your Java application using something like JSmooth.
Look at this question for a discussion of creating such binaries.
That's because Java applications aren't actually executable they're ran by the Java virtual machine which is why java appears in the process monitor, it's the host of your application.
Things like LimeWire however do but I think that's more down to GCJ - http://gcc.gnu.org/java/
I have a Java program that is mostly GUI and it shows data that is written to an xml file from a c++ command line tool. Now I want to add a button to the java program to refresh the data. This means that my program has to call the c++ functionality.
Is the best way to just call the program from java through a system call?
The c++ program will be compiled for mac os and windows and should always be in the same directory as the java program.
I would like to generate an executable can the c program be stored inside the jar and called from my program?
If you have access to the code and want an 'interactive' experience with the external program (e.g., make call, get results, make additional calls), investigate JNI, which allows you to call C or C++ code from a Java application by including & linking JNI juice to your C or C++ app with .
See:
http://en.wikipedia.org/wiki/Java_Native_Interface
http://www.acm.org/crossroads/xrds4-2/jni.html
If you really just need a "launch app and get results" sort of solution, check out Runtime.exec(), which lets you launch an external program & capture its output.
See:
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
http://www.rgagnon.com/javadetails/java-0014.html
Assuming no better communication method is available (SOAP, ICE, Sockets, etc), I'd call the executable using Runtime.exec(). JNI can be used to interface directly, but I wouldn't recommended it. No you can't put an executable in the jar. Well you can, but you can't run it, since the shell doesn't know how to run it.
You may also want to look at the Java Native Access API (JNA).
To answer your final question, you can't run an executable from within your jar.
However, you can store it within your jar and extract it to a temporary directory/file prior to running it (check for its presence the first time and extract if necessary). This will simplify your distribution somewhat, in that you only have the jar to distribute, and ensures that you're running an executable that matches your jarred Java code.