I have a java application in which i need to run the commands as we do on the terminal using the java code.How can i achieve this please suggest.
I Used this code when doing it on windows :-
Process p = Runtime.getRuntime().exec(command);
You use the following:
Process p = Runtime.getRuntime().exec(command);
All you need to do is change the commands to Mac OS X commands.
Related
I have a piece of Java code in which I am using Java Processbuilder to run powershell command on a remote machine. The remote machine is Windows but can I run this from Mac? I don't have Mac so I can't test it but users might have.
String[] commands = new String[] {"powershell.exe", "invoke-command", "-computerName", "myCom"};
new ProcessBuilder(commands);
Thanks
It won't work. I was thinking I can invoke the powershell somehow to run on the remote machine rather than the client itself. But I didn't find a way to do so . So had to use that code for Windows and write a separate one for Linux.
I am currently developing a Java system that communicates with node.js using socket.io. The system and the script are on the same server. How can I execute the script from my Java code and keep it alive from my app?
Note when using process builder the path to your JavaScript file is an argument and "node" is the command, so they need to be separated:
ProcessBuilder pb = new ProcessBuilder("node", "app.js");
This is also useful for inheriting its console output, starting the process and getting the reference to the process:
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
Say you have a node.js script named "mynodejs.js". You can use the following java code:
Process p = Runtime.getRuntime().exec("node mynodejs.js");
or
ProcessBuilder b = new ProcessBuilder("node mynodejs.js", "-args");
Have a look at ProcessBuilder class.
You can start any process on your machine like you would via shell, if I understand your question correctly.
I am trying to execute make clean command from a Java program.I need to change the directory first and then execute it.Or is there any other method?
I have tried both the ways as stated in the site.
1. using ProcessBuilder
ProcessBuilder pb = new ProcessBuilder("make "," clean");
pb.directory(new File("\\home\\p\\lipta-v1-3"));
pb.start();
Using Runtime
Runtime runtime = Runtime.getRuntime();
String command="make clean";
process=runtime.exec(command,null,new File("/home/p/lipta-v1-3"));
However none of them are working.The other commands such as cp ,rm etc.are working fine.
I am using Netbeans7.0.1 and ubuntu 12.04
I'm trying to, through a java program, write commands in the gnome terminal. I tried this code:
String cmd = "ls";
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
I'm trying to write "ls" to the terminal but nothing happens, but if I use
String cmd = "gnome-terminal";
I can open a new terminal window.
What I really want to do is run a C program from the terminal, calling it with java.
Thanks in advance.
gnome-terminal takes the -e argument, which allows you to tell it to execute a program.
gnome-terminal -e /path/executable
Just put them in a String[] and call the same method.
executing an external program works for me with the following commands:
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ls -l");
InputStream in = proc.getInputStream();
OutputStream out = proc.getOutputStream();
InputStream err = proc.getErrorStream();
proc.destroy() ;
}
Or, something similar is solved here: Executing in java code an external program that takes arguments as well.
If you're starting your java program from within a terminal then, after you've called exec() on the runtime, and get a Process you need to call the getInputStream() to read the output of the command, then you can print it out to System.out.
How I can read in this forum:
http://www.linuxquestions.org/questions/linux-general-1/run-command-in-new-gnome-terminal-185216/
you can use
gnome-terminal -x sh -c "ls"
to open a terminal and execute "ls" (if i remember, the "-c" option can execute a program in a new terminal.)
In this moment I'm at work and I haven't linux X system to try here. Sorry :)
I hope that this can help you!
I'm using Java 1.6 , Eclipse , Windows 7.
I'm trying to run commands in a java program to use NMAP.
The code :
String cmd[] = { "cmd.exe", "/c","start notepad.exe"};
Process pr = rt.exec(cmd);
works fine, but the code:
String cmd[] = { "cmd.exe", "/c","start nmap.exe"};
Process pr = rt.exec(cmd);
simply doesn't.
I tried both commands in the command prompt, they both work fine but the latter one fails when I try it with the Runtime.exec(). What would be the problem ?
Thanks, in advance..
Maybe "When Runtime.exec() won't" can help you.
The reason the command works in a command shell and not in Java might be that the command shell has the advantage of being able to refer to the PATH environment variable to find it; Java cannot. I'll bet if you put the full path to Nmap.exe that you'll fare better.