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

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.

Related

Run a bash script with "source XXXX.sh" by java [duplicate]

I am having a bash script file which I am calling using the source command in a shell and is setting a number of environment variables. Then I can use all the tools the environment variables are setting.
Now I want to do the same in Java by the use of:
static Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();
I know that source is an internal command and I can not call it from Java.
Is there any other way to set the enviroment variable in that file from java in order to be able to use them in my code later for calling other commands?
Thank you in advance!
Process pr = new ProcessBuilder("/bin/bash", "-c", ". env.sh; " + command).start();
Try something like this, where you both source the script and execute a subsequent command in the same shell process. Effectively you source the script every time you want to execute a command.

Terminal command fails in Runtime.exec()

Not sure if this is best for Stackoverflow or Superuser since it pretty much applies to both...
I'm running Lubuntu and I want to create a Windows-like 'start menu' search for the LXDE desktop menu. As with Windows, after an item has been found, I want to be able to launch it.
The LXDE menu system works with .desktop files, so my code performs the following command using Runtime.exec() to start up the programs:
gtk-launch <.desktop filename without extension>
This runs great for 99% of the time. Unfortunately I cannot figure out why that other 1% refuses to launch. One of these applications is TeamViewer. Now here comes the strange part: When I run the command gtk-launch teamviewer-teamviewer10 in a terminal, it works great, but if I run the same command through Runtime.exec(), it does not start, but it does not give me any error messages either.
Here is my code:
Process p = Runtime.getRuntime().exec(new String[] { "gtk-launch", "teamviewer-teamviewer10" });
p.waitFor();
Do I have to modify the gtk-launch command or is there something wrong with my code? (Note: Almost all other programs launch just fine.)
As a workaround, I decided to extract the Exec command from the .desktop file and run this through bash.
String command = getExecCommandFromDesktopFile().replaceAll("\\\\ ", " ");
Runtime.getRuntime().exec(new String[] { "/bin/bash", "-c", command });
This seems to work under all conditions. I still haven't figured out why gtk-launch does not work for all cases, but for me, this workaround will do just fine.

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" } );

Write to gnome terminal in 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!

Run commands on mac with java code

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.

Categories

Resources