I'm currently using an external editor of Matlab .m files, with a custom build system that calls Matlab from the command line to run the Matlab script (with the -nosplash and -nodesktop). However this creates two problems:
1) Matlab closes right after running the script: any windows or plots I call in the script are closed right after running the script, which obviously happens in a matter of seconds.
2) There is a slight delay every time I run the script because Matlab is effectively being started from scratch.
So I was wondering if would be possible to have Matlab running in the background, and just running the scripts whenever I want?
I'm running Linux 64bits, Matlab 2013a, and Sublime Text 3.
EDIT: I've testing the setup with a basic script:
a=5;
figure
plot(a);
EDIT2: I'm calling Matlab through a Sublime Text build system that runs:
matlab -nosplash -nodesktop <[script].m
There is no way to have Matlab running in the background and "just running the scripts whenever you want" without having an interactive session open somewhere.
Suppose that your system has a custom wrapper matlab-wrapper that is used to submit scripts in the background. You would call your script like this:
$ matlab-wrapper myscript.m
Likely, matlab-wrapper is doing something like this:
#!/bin/bash
/apps/matlab14a/bin/matlab -nodesktop -nosplash -r run\ "$1",exit
Or even more, submitting the above script to a scheduler via qsub or some other command.
The key would be modify the wrapper script to find the part where the Matlab binary is actually invoked. If your system allows, you could copy the wrapper script and modify it. (Either by simply removing the -r run\ "$1" text or something more complicated.) Then, you should be able to launch an interactive version of Matlab per the custom configuration on your system, and call your scripts from the Matlab command window.
Related
I'm trying to code a shell script to start/stop torrents using vuze's console UI through SSH:
https://wiki.vuze.com/w/Console_UI
I've downloaded vuze and everything works fine until I type this command:
java -jar Azureus2.jar --ui=console
After that, no command in my script works unless I quit that console.
Any solutions please? And if it's not feasible using shell scripts, any suggestions please?
Thanks.
Basically, the moment you run that command, your java program runs 'in the foreground', which means the rest of your script stops executing until your program exits.
If you want to keep on running the rest of your script while your java program executes you have to run your program in the background. One way to do that is as #Alp suggests:
java -jar Azureus2.jar --ui=console &
The following process normally works for my startup scripts. However, when I introduce a command to execute a JAR file, it does not work. This script works while I am logged in. However, it does not work as a startup script.
In /etc/init.d I create a bash script (test.sh) with the following contents:
#!/bin/bash
pw=$(curl http://169.254.169.254/latest/meta-data/instance-id)
pwh=$(/usr/bin/java -jar PWH.jar $pw &)
echo $pwh > test.txt
Make script executable
In /etc/rc.local, I add the following line:
sh /etc/init.d/test.sh
Notes:
I make a reference to the script in /etc/rc.local, because this script needs to run last after all services have started.
Please do not ask me to change the process (i.e., create script in /etc/init.d/ and reference it from /etc/rc.local), because it works for my other startup scripts.
I have tried adding nohup in front of java command, and it still did not work.
Thanks
As written, there is insufficient information to say what is going wrong. There are too many possibilities to enumerate.
Try running those commands one at a time in an interactive shell. The java command is probably writing something to standard error, and that will give you some clues.
Let's say I have a Mac application bundle and my executable inside isn't Cocoa/Objective-C based but I use a normal shell script for any good reason:
myProgram.app/
Contents/
MacOS/
mainExecutable.sh
Resources/
The shell script does some foo before starting a java process with the main program (GUI based).
I now want to receive drop events to the Apps Dock icon in my java process, but that doesn't seem to work.
The shell script neither receives the dropped file(s) as argument list on startup ($0 or $#) nor does it receive any input on stdin.
How can I pass the drop events and the list of dropped files to the shell script and any executed program inside?
After doing long researches I found the answer on the apple mailing list:
All I need to add to the sh script is this line before invoking the java command:
export CFProcessPath="$0"
And this really works like a charm!
This is my final code:
...
export CFProcessPath="$0"
exec java -jar ...
And as the question on the apple mailing list was another one, this not only works for handling drop events in the shell script but also for supporting OSX System Services thru JNI.
This is a really bizarre application, so bear with me. The primary application consists of a Matlab back-end with a UI written in Java Swing. To launch the application, you do something like the following from the command-line:
matlab -r "initMatlab;runJavaUI;"
Assume that initMatlab and runJavaUI are both Matlab functions on the path of the matlab runtime that gets spawned as a result of that command. This basically spawns an instance of matlab, then runs those two matlab functions immediately after matlab starts. The weird thing is, this command-line option doesn't work on all OSes. Furthermore, not all versions of Matlab are compatible with all versions of Java (our customer wants to use REALLY old Matlab installs... can't make 'em change it).
So ... we provide a Java executable Jar that automatically generates the command line arguments based on which OS you're running in - the user just double-clicks it to bring up a small UI, then is presented with a few options. I'll call this jar the Launcher. This Launcher detects all installed instances of Java and Matlab and allows the user to pick which combination of Java and Matlab will get used. The jar uses ProcessBuilder to launch the matlab command, which works fine, save for one thing. In Windows, the Launcher starts Matlab just fine, we see the Matlab console appear, then the Java UI appears as it should. However, on a Mac running Mountain Lion (10.8, I believe), the Matlab console never appears - this isn't to say that Matlab isn't running though - I can still make Matlab calls from the Java UI. This doesn't prevent the user from using the UI, but sometimes Matlab will dump error messages to the console which is problematic, because on a Mac, the user will never know when something is going wrong. The weirdest thing is, I have tried printing out the generated command line, which doesn't spawn the Matlab console when launched via ProcessBuilder, but does spawn the Matlab console when launched via terminal. Once again, Matlab isn't failing to start, it's failing to make its own UI visible.
I have tried copying all of the Launcher's environment variables into the ProcessBuilder prior to launching Matlab, to no avail. So I have to leave this a bit open-ended, but has anyone tried running Matlab using ProcessBuilder, and if so, what did you do to get the Matlab console to appear on Mac OS Mountain Lion?
SSCCE
Export this code into a runnable jar file, then run the following on Mac OS Mountain Lion:
java -jar launch-matlab.jar /absolute/path/to/matlab
You should see the Matlab icon appear in the dock, but you won't be able to make the Matlab console window visible. If you run this in Windows, the Matlab console appears as it should.
public class LaunchMatlab {
public static void main(String[] args) throws Exception{
String matlabExe = "matlab";
if(args != null && args.length > 0) matlabExe = args[0];
ProcessBuilder pb = new ProcessBuilder();
pb.command(matlabExe,"-wait");
pb.environment().putAll(System.getenv());
System.err.println("Launching Matlab using following PB args: "+
pb.command());
Process p = pb.start();
System.err.println("Waiting for Matlab to exit ...");
p.waitFor();
System.err.println("Matlab exited, launcher exiting ...");
}
}
You need to specify that MATLAB is to run with a visible UI by making use of the -desktop flag. I don't believe this is documented.
So the command you need to run is
matlab -desktop -r "initMatlab;runJavaUI;"
I've created a Java library called matlabcontrol that can abstract all of this away from you. It can launch MATLAB while running on Windows, OS X, and Linux and then allow you to interact with MATLAB via eval and feval commands. matlabcontrol's code to launch MATLAB is located in matlabcontrol.RemoteMatlabProxyFactory's createProcess(...) method. If you make use of matlabcontrol as a Java library you won't directly interact with either this class or this method.
I'm using Sublime Text 2, and I want to be able to compile and run Java Files with one button.
When running Windows, the Batch file Required is:
#ECHO OFF
cd %~dp1
javac %~nx1
java %~n1
I'm wondering what that would look like in a Shell Script, cause I don't know much about Shell Scripts...
I'm using the Open JDK and JRE in case it matters.
Thanks for the help,
Kelan
The bash equivalent to that script would be something like:
#!/bin/bash
cd "$(dirname "$1")"
javac "$(basename "$1")"
java "$(basename "$1" ".${1##*.}")"
A shell script would STILL require running a batch file to start it. For example, you could write a Ant task that could do what you are asking, or perhaps a Beanshell script. But, you still wont be able to avoid making a batch file that launches it. The batch file that you are using now is as close as you'll get to a single click solution. Not even a PowerShell script would give you better convenience.