views:

82

answers:

7

I need to execute a batch file which executes another Java application. I don't care whether it executes successfully or not and I don't have to capture any errors.

Is it possible to do this with ProcessBuilder? What are the consequences if I do not capture errors?

However, my requirement is just to execute another Java application.

+1  A: 

I assume that you know how to execute the command using the ProcessBuilder.

Executing a command from Java always should read the stdout and stderr streams from the process. Otherwise it can happen that the buffer is full and the process cannot continue because writing its stdout or stderr blocks.

mklhmnn
IS there any way to just start the another app and ignore stdout and stderr streams from the process.
Maybe you create a `bat` file which redirects the output of the executed command to "dev-null".
mklhmnn
+2  A: 

If you don't care about the return value you could just use Runtime.getRuntime().exec("path.to.your.batch.file");

James B
what about stdout and stderr streams from the process getting full.
Without knowing what you want to run, it's a little difficult to tell. I've seen programs such as SQL Server's BCP not report errors properly when exec'd and asking for the stdout and stderr streams, so it kind of depends on your child process. If it doesn't have any output, then don't worry about it, the stdout and stderr will be garbage collected along with your `Process` object when the process is finished
James B
I have a simple question, what if as soon as I execute another java app, can't I do process.destroy. Doesnt that help stdout/err getting full, or I am talking about totally diff thing. Because, once the another app is started I can quit my app.
@user234194that Process object that is returned by exec is your window onto that process. If you destroy that `Process`, that will destroy your child process too. if you just want to kick off the java app, then your batch file could be something like `cmd java <com.your.child.process.that.you.want.to.run.class>` as this will create a further (windows) process (Java won't know about it) that will return immediately to your java app that called Process.exec from...but be warned, this is a bit of a hack, and may look weird to someone else picking up your code afterwards
James B
WHen I meant, running another app that meant thru batch file, even though if I do that, what about stdout and stderr.And what about capturing error.
you can ignore them if you want to, you said in your original post that you didn't care about them. if you don't care about them, then don't open the streams to them in the `Process` object. Like most of these kind of things, try it and see how it goes, you can knock together a quick test class in about 4 lines of code (8, if you include the close curly brace on a new line)
James B
A: 

http://www.devdaily.com/java/java-exec-processbuilder-process-1
this may be helpfull

christian
A: 

You could simply use Runtime.exec()

Gopi
A: 

The Runtime.getRuntime().exec() approach is quite troublesome, as you'll find out shortly.

Take a look at the Apache Commons Exec project. It abstracts you way of a lot of the common problems associated with using the Runtime.getRuntime().exec() and ProcessBuilder API.

It's as simple as:

String line = "myCommand.exe";
CommandLine commandLine = CommandLine.parse(line);
DefaultExecutor executor = new DefaultExecutor();
executor.setExitValue(1);
int exitValue = executor.execute(commandLine);
StudiousJoseph
A: 

You can execute a batch instruction, or a any application using

Runtime.getRuntime().exec(cmd);
  • cmd is the command or te application path.

Also yo can wait for executiong and getting the return code (to check if its executed correctly) with this code:

  Process p = Runtime.getRuntime().exec(cmd);
  p.waitFor();
  int exitVal = p.exitValue();

You have a full explanation of different types of calls here http://www.rgagnon.com/javadetails/java-0014.html

Dubas
+1  A: 

Yes it is possible using ProcessBuilder.

ProcessBuilder example:

import java.io.*;
import java.util.*;

public class CmdProcessBuilder {
  public static void main(String args[]) 
     throws InterruptedException,IOException 
  {
    List<String> command = new ArrayList<String>();
    command.add(args[0]);


    ProcessBuilder builder = new ProcessBuilder(command);
    Map<String, String> environ = builder.environment();

    final Process process = builder.start();
    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
    }
    System.out.println("Program terminated!");
  }
}

Check these examples:

http://www.rgagnon.com/javadetails/java-0014.html

http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

YoK