Open a Terminal window with Java - java

I am aware that you can pass commands to the Terminal and it returns your result in Java. The problem is for undisclosed reasons I need a Terminal window to open and to have the command in it. This code works to open an empty Terminal window (on mac):
p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal");
And I have seen someone say on a different overflow question that you can do this:
p = Runtime.getRuntime().exec("/usr/bin/open -a Terminal [Put Command to pass through here]");
But it didn't seem to work when I tried to pass the command through to the window.
Any help or suggestions?

You can achieve this by using AppleScript.
Here you can find example how call it from Java:
AppleScript from Java on Mac OS X 10.6?
And script you need will look like this:
tell application "Terminal"
set currentTab to do script ("cd ~/Downloads/")
set currentTab to do script ("ll")
end tell

Related

Using PHP (may be commandline using shell_exec() function), how can I determine if a Java application/jar file is currently running or not?

I have a PHP application which executes a Java .jar file through shell_exec():
shell_exec("java jar myJarProgram.jar");
I have a need to determine if an instance this java program myJarProgram.jar is already running, because if it is not, then I can start it using the above PHP statement.
How can I do that?
You can use "jps" utility to grep your process with java
jps -mlvV | grep myJarProgram.jar
jps is a good candidate for this, but please note that
To use the jps command-line tool you need to install a JDK.
Otherwise, you can parse the output of another shell_exec call that uses ps with the arguments you want in order to get the running processes: at this point you can check if the process is present.
$search_string = "[j]ava jar myJarProgram.jar";
$running = shell_exec("ps -A -ww | grep '$search_string'");
or similar.
If $running is empty, you can launch the jar.
Another option is to perform everything with a single shell_exec, both with commands concatanation (simple && and ||) or creating a .sh script and shell_executing that.
EDIT:
According to the user comment, the script must work both for Windows and Linux.
You can use the php PHP_OS predefined constant to check if it's Windows or Linux:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') etc....
If it's Linux, you can use the shell_exec as reported above.
If it's Windows, you can change the shell_exec string using the tasklist Windows command. I don't know it, but there are already dedicated questions and answers like this one.

Bash script does not end Java process clicking on the "X"

Can someone explain to me why this is the case. Let's say I have a simple java program that loops forever until I kill the process.
If I make a shell script like this.
#!/bin/bash
# clears the terminal
clear
# runs the java executable
java -jar java_program.jar
# prevents the terminal from closing
$SHELL
The shell script runs the program and if I use hotkey CTRL + C it ends the java process exactly how I want it. But when I use the "X" in the top right corner the java process does not end. Why does it do this how do I solve this issue?
I know the java process does not end because if I look in task manager the process is still running.
I am using windows 10.
I do not have git bash installed, but the behaviour you perceive comes from the terminal emulator that is used around git bash. It is not the windows native terminal emulator (and running cmd), it is not the cygwin terminal emuator.
Since you have it installed figure out the software emulating the terminal and see how that treats the processes when you close the terminal window. Maybe it is configurable.

Add arguments to runnable jar file Mac OS

I have created little java program with SWT.
For run it on Mac I need to use argument XstartOnFirstThread.
So from terminal I can start it like
java -XstartOnFirstThread -jar progname.jar
Can I open this program using double click with this parameter?
I know I may create script to run it, but is this possible without script?
There is a related question here.
The answer posted by Peter Lawrey states it outright: There is no way to pass the argument when starting the .jar by double-clicking.
However, there are alternatives:
The double click starts a wrapper program which then starts your program with the parameter (as suggested in the linked answer).
Create a script file that launches your application.
Create a proper OSX application instead.

Execute Command Line Java program in background

Is it possible to execute a Java program in the background so the user can execute other commands in front of it?
For instance, here is how the console might look for said program:
$ myProgram (executes program)
Program Started! (output from myProgram)
$ (the user can enter another UNIX command while myProgram is still running)
Thanks in advance!
Background execution is part of the shell. You can add & at the end of the command line to run it in the background.
The background output does not go to the current shell. If that happened, it would be confusing to the user, having to type input while the terminal is still producing output.
EDIT: I just tried "ls &" on cygwin, and the ls output appears in the console. Seems there is a lot of conflicting information on the net! :)
Best way is to use screen if you dont have it type
sudo apt-get install screen
type
screen
run the command like
java MyClass
press ctrl + (a + d)
to view this window again, type screen -x
I beleive it's possible to start the program and allow access to the shell... but the user would not see the output.
I do not think its possible to achieve the example scenario you have given.

How i run cygwin using java program?

I have installed cygwin on window to start crawling. It work well.
Now i want to run cygwin and run a crawl program at starting of cygwin using java program code.
If you provide some code for it ,it will be great help for me.
I looked at adatapost's link. It seems like a world of trouble awaits you down this path.
I mean, I like Cygwin a lot, but I wouldn't use it like this.
A few centimetres to the right of the 'Your Answer' box I'm typing in is a link to a Related question 'How can I run cygwin from Java?'
Who's putting the cart before the horse? I don't know.
Does Cygwin have to be involved at all ?
If you are trying to run a binary that requires the cygwin1.dll (which includes most commands you can execute from the cygwin bash shell) then you can run it by specifying the cygwin\bin directory in the path environment variable like this:
Process p = Runtime.getRuntime().exec("C:/path/to/cygwin/binary.exe", new String[] { "PATH=C:\\cygwin\\bin" });
This assumes you installed cygwin in C:\cygwin

Categories