How to open IE from a java program - java

Hey I tried to open IE from a java program.
the command start iexplorer works on command prompt and terminal but when used in a java program throws a IOException. When I execute the command cmd start iexplorer the program is just running without stopping for almost 15 minutes
String command = "cmd start iexplore";
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
This is the call stack of from VScode:
image
can someone help me with this please

All you are doing is running "cmd" in background. If iexplore is on your system Path then this may work:
String[] cmd = new String[] {"cmd", "/c", "start iexplore"};
The "/c" option tells CMD.EXE to start the process, and then CMD exits immediately [in your case it is hanging around for more input]. Also you need to read the STDERR stream of the process to see any error messages from CMD.

Related

Opened external JAR file with JAVA ProcessBuilder does not work properly

I want to open JAR file by pressing JButton in JPanel. To achieve this goal I used ActionListener with ProcessBuilder inside. Here is my code:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ProcessBuilder pb = new ProcessBuilder("java", "-jar"
, "f:/Documents/TBot/topbotclient.jar"
, "-n", getTopBotName().getText()
, "-pw", getTopBotPass().getText()
, "-s", getScript_name().getText()
, "-w", getWorld().getText()
);
try {
Process p = pb.start();
} catch (IOException ee) {
ee.printStackTrace();
}
}
});
The problem is that opened JAR file do not function properly - it freezes after I press some of its buttons. However, if I close my initial JAVA window that is used to open external JAR, JAR file becomes functional again. What to do to get both initial window and opened JAR file window functional?
I did not find any solution by searching:
Run a jar File from java program,
Execute .jar file from a Java program etc.
UPDATE:
I tried not to use ProcessBuilder and used "runtime.exec" instead.
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec(" java -jar f:/Documents/TBot/scripts/topbot.jar -n Fataho -pw diehard15 -s scriptjoiner -w 301 -nort -a b#hadas.lt -apw blood444");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Exception occured" + ex);
}
The problem remains the same.
Try to start a processbuilder in a separate thread so that your main thread won't block. From your code your doing everything in a Main thread. Make use of swingWorker kind of stuff to start the operation in a separate thread.

firing a command in terminal from java program

I need to write a java program which when executed pushes a command into the terminal
I tried using runtime.exec(); but not working fine for me
what i want is "/home/raj/Desktop/java -jar test.jar" to be executed in terminal
Can any one help me to sort it out.
If you want to actually start a terminal window (rather than just executing the java process) you will need to launch xterm (or something similar) and tell xterm to run java for example
String command= "/usr/bin/xterm -e /home/raj/Desktop/java -jar test.jar";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);
Please refer following example .with list of arguments to java program.
Process proc = null;
try {
String cmd[] = {"gnome-terminal", "-x", "bash", "-c", "ls; echo '<enter>'; read" };
proc = Runtime.getRuntime().exec(cmd, null, wd);
} catch (IOException e) {
e.printStackTrace();
}
You can use full path of the jar file as an argument to "java"
String command= "java -jar /home/raj/Desktop/test.jar";
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

How to backup a mysql db from java using runtime/process classes?

I have a problem with this code:
try {
String cmd = "C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe -uroot -proot foo_db -rC:\\backup.sql";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
} catch(Exception e) {}
It doesn't get any error, pratically it doesn't DO anything: no backup and no execution of the cmd. Where's the problem?
It's strange, because the cmd-text is correct... i've tried to do it with 'Execute' command in windows and it works, but in java no.
Thanks in advance.
Your first problem was, as #pcalcao pointed out, that you are not reporting the exception. You really should never do this. At the very least you should do:
} catch(Exception e) {
e.printStackTrace();
}
java.io.IOException: Cannot run program "C:\Program": CreateProcess error=193, %1 isn't a Win32 valid application
That says that you have a problem with your application path. By default, if exec() is called with a single argument, then it will break the arguments up by spaces. Since you have spaces in your path you need to pass an array of strings to exec(). Something like:
try {
String cmd =
"C:\\Program Files\\MySQL\\MySQL Server 5.5\\bin\\mysqldump.exe";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(new String[] { cmd, "-uroot", "-proot", "foo_db",
"-rC:\\backup.sql" });
// wait for it to finish
proc.waitFor();
} catch(Exception e) {
e.printStackTrace();
}
The first argument in the string array passed to exec() is then the full path to the command -- this can have spaces. Each of the other array elements is an argument to the command.
Lastly, you will need to wait for the process to finish otherwise it will execute in the background. That's what the waitFor() does.
You need to escape the whitespace with \, exec is trying to execute "C:\Program", from what you've showed in your answer to my previous comment.
NEVER leave a catch clause empty.

Execute Unix system command from JAVA problem

I am facing a weird issue with executing a system command from JAVA code.
Actually i want to get the Mac OSX system information from my JAVA App.
For that im using
Runtime.getRuntime().exec("system_profiler -detailLevel full");
This is working fine.If i print the output,it is cool.
But i want to write this information to a plist file for future use.For that im using the -xml argument of system_profiler.like,
String cmd = "system_profiler -detailLevel full -xml > "+System.getProperty( "user.home" )+"/sysinfo.plist";
Process p = Runtime.getRuntime().exec(cmd);
Basically this should create a plist file in the current users home directory.
But this seems to be not writing anything to file.
Am i missing something here ?
My Java is more than rusty, so please be gentle. ;-)
Runtime.exec() does not automatically use the shell to execute the command you passed, so the IO redirection is not doing anything.
If you just use:
"/bin/sh -c system_profiler -detailLevel full > path/file.plist"
Then the string will be tokenized into:
{ "/bin/sh", "-c", "system_profiler", "-detailLevel", "full", ">", "path/file.plist" }
Which also wouldn't work, because -c only expects a single argument.
Try this instead:
String[] cmd = { "/bin/sh", "-c", "system_profiler -detailLevel full > path/file.plist" };
Process p = Runtime.getRuntime.exec(cmd);
Of course, you could also just read the output of your Process instance using Process.getInputStream() and write that into the file you want; thus skip the shell, IO redirection, etc. altogether.
Christian.K is absolutely correct. Here is a complete example:
public class Hello {
static public void main (String[] args) {
try {
String[] cmds = {
"/bin/sh", "-c", "ls -l *.java | tee tmp.out"};
Process p = Runtime.getRuntime().exec (cmds);
p.waitFor ();
System.out.println ("Done.");
}
catch (Exception e) {
System.out.println ("Err: " + e.getMessage());
}
}
}
If you weren't using a pipe (|) or redirect (>), then you'd be OK with String cmd = "ls -l *.java", as in your original command.
If you actually wanted to see any of the output in your Java console window, then you'd ALSO need to call Process.getInputStream().
Here's a good link:
Running system commands in Java applications

Queuing Nmap Commands in a visible command prompt window in Java

I am trying to run multiple Nmap commands one after another.
Ideally, each Nmap command will be created in its own command prompt window. The Nmap command will execute and finish. Then another command prompt will appear with the next Nmap command, execute, and so on and so forth.
Unfortunately, the the way the program currently runs, multiple command prompt windows pop up at the same time and execute simultaneously. I want the commands to execute only one at a time. I had thought that the waitFor() method would solve the problem, yet it hasn't. Am I missing something?
I have simplified this greatly from my actual program to solve the core issue. Any help would be appreciated.
try {
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "start", "nmap", targets, "-p 1- 65535", "-oN +output");
Process p = pb.start();
p.waitFor();
System.out.println("p done");
Process z = pb.start();
z.waitFor();
System.out.println("z done");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is because you are doing a "start". The process will start another one to handle the nmap command, and terminates without waiting for that child process to terminate.
Removing the "start" should make the windows show one after the other.

Categories

Resources