Running the program with out GUI - java

I have a requirement in my project that I have to run external program with out GUI even if external program has GUI(like notepad.exe).This is possible in .NET. But my program is in java. I found out that java does not have this facility.
Please let me know is there any program like hstart(which only hides the console not GUI) which hides the GUI if program is given as parameter(Ex: [hstart] notepad.exe).

If you know how to do that in .NET write a little program for this yourself and call it from your java app.

Same idea as previous answer, but you can call through a vbs script.
For example, you have "run_and_hide.vbs"
Set WshShell = WScript.CreateObject("WScript.Shell")
Return=WshShell.Run(Wscript.Arguments(0),0,false)
Then execute the script from java code and pass the target program in the first argument:
Runtime.getRuntime().exec("cmd /c start run_and_hide.vbs " + "\"notepad\"");

Related

run Java commands through a Shell script

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 &

Run bash file from java

I want to run bash file from java, i am using play framework.
any idea how to do that??
is this code enough:
new ProcessBuilder("pathToYourShellScript").start();
Possible duplicate of How to run Unix shell script from Java code? (don't have the reputation to put that in a comment, sorry).
In a nutshell - your line of code will work and the shell script will run by just doing that (tested on a SUSE Linux box).
However, if you want to do anything with the output or to know that it has completed, you'll have to add more code to check its state (check out the Process.waitFor() API) or to capture its output in Java (e.g. by capturing Standard out in a BufferedReader) - check Process.getInputStream() API.

Correct way to run another java program from java

I want to run the following shell command from a java application:
java -jar saxon9he.jar -warnings:fatal a.xml a.xsl param1=123 param2=abc
Currently, I am simply executing this as a shell command using
ProcessBuilder pb = new ProcessBuilder(commandLineParts);
[...]
Process process = pb.start();
What is the correct way to do this in java?
This is the correct way of executing a command in Java. Just to clear possible confusion: ProcessBuilder doesn't execute the program using a shell. That's the reason why you have to provide it with a list of arguments and not a single string (that would be parsed by a shell).
There are two possibilities:
either you want to run the Java program in a new JVM and then using
the ProcessBuilder is the way to go
or you don't mind if it is executed in the same JVM and then you can call the main method yourself as Sean suggests (possibly in a different thread)
Another option, depending on the type of the application, would be to perform some acrobatics with an application server to start the app in it.
If you use the ProcessBuilder just be careful about handling its input and output streams - if you don't handle them your application can hang: Java ProcessBuilder: Input/Output Stream This has been improved in Java 7.

Launching Matlab from java ProcessBuilder, Matlab console doesn't appear in Mac OS 10.8

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.

exec command in java

If i am using following command in java:
Process ps = Runtime.getRuntime().exec("some .exe file of VB");
How do I know that the particular .exe has done its job eg: it executed successfully.
How do i know that it has some error or just completed half task in java.
How should I design my program in java to know or Is there any way to tell java from VB.
Any help is appreciated.
I would assume that you could look at the exit status of the program: ps.exitValue() or you could read the stdout/stderr ps.getInputStream() / ps.getErrorStream() respectively.
You get back a Process
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/Process.html
Which has such methods as:
exitValue()
getErrorStream()
waitFor()
Which will get you what you need

Categories