Write to gnome terminal in java - java

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!

Related

Runtime.getRuntime().exec() doesn't execute some commands

I'm beginner at java and have some problems. I've read several topics about this theme but none of them worked for me. Here is my code:
try
{
Console console = System.console();
String command;
while(true)
{
command = console.readLine("Enter input:");
Process proc = Runtime.getRuntime().exec(command);
// Read the output
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
}
}
catch(Exception e) {}
So what I'm trying is to make a java program and run terminal commands in it(I'm using linux). This program works with commands like "ls" "ps ef" and others but it doesn't work when I type "cd". I know that cd makes different process and should be used this way: "Runtime.exec(String command, String[] envp, File dir)". My questions is:
How to make my program run all kinds of terminal commands? Sorry if question sound silly. Thank you.
The cd command is a shell built-in command. There is no shell when you run a command via exec(...). Indeed, if you try to find a cd command in any of your system's bin directories, you won't find one ... because it is impossible to implement as a regular command.
If you are trying to use cd to change the current directory for the JVM itself, that won't work because a command can only change the current directory of itself and (after that) commands that it launches itself. It can't change its parent processes current directory.
If you are trying to use cd to change the current directory for subsequent commands, that won't work either. The context in which you set the current directory ends when the command finishes.
In fact, the right way to change the directory for a command run using exec is to set it via the ProcessBuilder API itself.
How to make my program run all kinds of terminal commands?
You can't. Some of the "terminal commands" only make sense as shell commands, and that means you need a shell.
I suppose, you could consider emulating the required behaviour in your Java code. That would work for cd ... but other commands are likely to be more difficult to cope with.
(For what it is worth, it is possible to implement a POSIX compatible shell in Java. It is just a LOT of work.)
you've actually got to run the console you want to use (ie sh, csh, bash, etc) and then use the process OutputStream to feed in commands
I think the Problem is not your Code, the command is the problem...
what do you want to see if your command is cd ??
In Background it changes the path but you get nothing back.
Changing the Directory is not processing any output.
This worked for me:
Runtime.getRuntime().exec(new String[]{ "/system/bin/sh", "-c", "ls -l" } );

java how to call external unix command

I would like to call a unix system command in java code, I write code like this:
String cmd = "split -a -d -b 10M test.txt";
Process execProc = Runtime.getRuntime().exec(cmd);
I hope when the external command is finished, I could continue to execute the following program. How to implement it?
Try
execProc.waitFor();
This should block until the command is finished. Also maybe you should consider using ProcessBuilder, it offers much more convenient API for handling processes.
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
process.waitFor();
InputStream stream = process.getInputStream();

Enter commands in the command prompt after it's been opened in Java?

I would like to know how to enter commands into the cmd.exe (command prompt window) , after its been opened?
I have the code below to open cmd.exe:
Runtime rt= Runtime.getRuntime();
Process process= rt.exec("cmd.exe /c start cd c:\\ExecutionSDKTest_10.2.2");
But after it's been opened, I'd like to enter "ant compile" or any line, how do I do that??
The normal way to do this would be to put the commands in a script and execute the script.
You will need to consume the output of the child process (stdout and stderr) on separate threads, or your process will block.
you dont need to open the command line to compile a program with a running program, check this out how to compile & run java program in another java program?
Not exactly the answer to you question. But you can use ProcessBuilder to set your process current directory (so you don't need to call "cd ..." anymore)
Try to pass a List to the ProcessBuilder
final List<String> l = new ArrayList<String>();
final String cmd = "C:/Program Files/Java/jre6/bin/";
l.add("C:\\WINNT\\system32\\cmd.exe ");
l.add("cd " + cmd);
l.add("dir");
l.add("java.exe -version");

Running commands in a terminal via exec in Java NOT as root

I've been playing around with exec and although it opens a terminal with the user being me, it doesn't seem to be able to execute commands I give it =/ code is as follows:
try{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("xterm -e \"source /home/USER/.bashrc; ~/./myscript.sh\"")
}
catch(Throwable t)
t.printStackTrace();
In the xterm console I get:
xterm: Can't execvp ": No such file or directory
user#user:$
Which is then a terminal waiting to be used, and will not go away until I ctrl-d it.
Not sure what's going on here?...
Thanks.
I don't think you can pass shell stuff into xterm's -e option. It wants a program and optional parameters for that program. source is a shell built-in.
Something you can try doing is just calling your myscript.sh and modifying it so that it sources your bashrc at the top. Or create a new bash script that sources your rc file then runs myscript.sh.

Problem with Java Runtime.exec() when trying to start Nmap.exe

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.

Categories

Resources