pass multiple parameters to ProcessBuilder with a space - java

I would like to pass multiple parameters to a processBuilder and the parameters to be separated by a space.
Here is the command,
String[] command_ary = {dir+"library/crc"," -s ", fileName," ",addressRanges};
I need to provide a space after "fcrc" and after "-p" and in between "filename" and the "addressRange".
Thank you

You don't need to include spaces. The ProcessBuilder will deal with that for you. Just pass in your arguments one by one, without space:
ProcessBuilder pb = new ProcessBuilder(
dir + "library/crc",
"-s",
fileName,
addressRanges);

We need spaces between arguments in commandline because the commandline need to know which is the first argument, which is the second and so on. However when we use ProcessBuilder, we can pass an array to it, so we do not need to add those spaces to differentiate the arguments. The ProcessBuilder will directly pass the command array to the exec after some checking. For example,
private static final String JAVA_CMD = "java";
private static final String CP = "-cp";
private static final String CLASS_PATH = "../bin";
private static final String PROG = "yr12.m07.b.Test";
private static final String[] CMD_ARRAY = { JAVA_CMD, CP, CLASS_PATH, PROG };
ProcessBuilder processBuilder = new ProcessBuilder(CMD_ARRAY);
The above code will work perfectly.
Moreover, you can use
Runtime.getRuntime().exec("java -cp C:/testt Test");
But it is more convenient to use ProcessBuilder, one reason is that if our argument contains space we need to pass quote in Runtime.getRuntime().exec() like java -cp C:/testt \"argument with space\", but with ProcessBuilder we can get rid of it.
ProcessBuilder processBuilder = new ProcessBuilder("command", "The first argument", "TheSecondWithoutSpace");

Use it like this:
new java.lang.ProcessBuilder('netstat -an'.toString().split('\\s'))).start()

Related

How can I get all errors when executing a command?

I'm developing a java program, at a certain point in the program I need to execute some commands and show all the errors returned by that command. But I can only show the first one.
This is my code:
String[] comando = {mql,"-c",cmd};
File errorsFile = new File("C:\\Users\\Administrator2\\Desktop\\errors.txt");
ProcessBuilder pb = new ProcessBuilder(comando);
pb.redirectError(errorsFile);
Process p = pb.start();
p.waitFor();
String r = errorsFile.getAbsolutePath();
Path ruta = Paths.get(r);
Charset charset = Charset.forName("ISO-8859-1");
List<String> fileContents = Files.readAllLines(ruta,charset);
if (fileContents.size()>0){
int cont = 1;
for(String str : fileContents){
System.out.println("Error"+cont);
System.out.println("\t"+str);
cont++;
}
}
else{
//other code
}
In this case I know that there are more than one errors, so I expect more than one output but as you can see in the photo I get only one.
I think the key here might be that ProcessBuilder's redirectError(File file) is actually redirectError (Redirect.to(file)) .
From Oracle's documentation of ProcessBuilder class:
This is a convenience method. An invocation of the form redirectError(file) behaves in exactly the same way as the invocation redirectError (Redirect.to(file)).
Most example's I have seen use Redirect.appendTo(File file) rather than Redirect.to(file). The documentation may explain why.
From Oracle's documentation of ProcessBuilder.Redirect :
public static ProcessBuilder.Redirect to(File file)
Returns a redirect to write to the specified file. If the specified file exists when the subprocess is started, its previous contents will be discarded.
public static ProcessBuilder.Redirect appendTo(File file)
Returns a redirect to append to the specified file. Each write operation first advances the position to the end of the file and then writes the requested data.
I would try replacing
pb.redirectError(errorsFile)
with
pb.redirectError(Redirect.appendTo(errorsFile))
and see if you get more lines that way.
Have you debugged and checked the contents of fileContents?
EDIT: Sorry, it should be a comment, but can't do it yet :(

How to pass absolute(full) path of a file to ProcessBuilder class

I'm trying to run process builder to execute a file which is inside my bin/resources/ folder of my java project. For that, I don't want to hard code the complete path, so I'm trying to pass absolute path (full path I mean) using class loader and pass this value as a list argument to ProcessBuilder class. How ever I'm not able to succeed in doing so.
Below code samples (both Case 1 and 2) run fine but nothing happening in the end. I mean the file is not getting called.
Case 1:
String rCmd = "Rscript.exe";
String rScriptName = "resources/MyScript.R";
List list = new ArrayList<>();
list.add(rCmd);
list.add(this.getClass().getClassLoader().getResource(rScriptName).toURI().toString());
ProcessBuilder pb = new ProcessBuilder(list);
pb.start();
Case 2:
String rCmd = "Rscript.exe";
String rScriptName = "resources/MyScript.R";
List list = new ArrayList<>();
list.add(rCmd);
list.add(this.getClass().getClassLoader().getResource(rScriptName).toString());
ProcessBuilder pb = new ProcessBuilder(list);
pb.start();
Below piece of code throwing exception:
Case 3:
String rCmd = "Rscript.exe";
String rScriptName = "resources/MyScript.R";
List list = new ArrayList<>();
list.add(rCmd);
list.add(this.getClass().getClassLoader().getResource(rScriptName).toURI());
ProcessBuilder pb = new ProcessBuilder(list);
pb.start();
Case 4:
File file = new File(this.getClass().getClassLoader().getResource(rScriptName).toURI());
List list = new ArrayList<>();
list.add(rCmd);
list.add(file);
ProcessBuilder pb = new ProcessBuilder(list);
pb.start();
Output:
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:361)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1005)
Below code gives me expected output but don't want to hard code the path this way, as I need to run this code in linux box later.
String rCmd = "Rscript.exe";
String rScriptName = "D:/MyScript.R";
ProcessBuilder pb = new ProcessBuilder(rCmd, rScriptName);
pb.start();
Expecting your much needed help on this!
String rCmd = "Rscript.exe";
String rScriptName = "filename Without Resource";
List list = new ArrayList<>();
list.add(rCmd);
list.add([Class name].class.getClassLoader().getResource(rScriptName).toURI().getPath());
Was working.
Process builder Expect String Argument So try to parse the list and it will give the below exception unless they are strings.
Try
this.class.getClassLoader().getResource(rScriptName).toURI().getPath()
It will add the absolute path of the file and it will execute.
Class ProcessBuilder

How to run java command through code without creating a new process?

Runtime.getRuntime().exec("....")
and
ProcessBuilder pb = new ProcessBuilder("java", "-server", "-jar", "yourJar.jar");
Process p = pb.start();
The above 2 ways of executing a command create a new process for running the command.
Is there a way to execute the command in the same process, without creating a new one?
As #soong commented, you could manually load your JAR and the classes you need, and then call the main method by reflection. You can achieve this with something like this:
// load your JAR file as a File instance
String myJarPath = "C:\\somefolder\\someOtherFolder\\MyJar.jar";
File myJarFile = new File(myJarPath);
// create a new class loader based on your JAR's URL
URLClassLoader classLoader = new URLClassLoader(new URL[]{myJarFile.toURI().toURL()});
// load the class with the main method
Class<?> classToLoad = classLoader.loadClass("MyClass");
// get the main method
Method method = classToLoad.getMethod("main", String[].class);
// invoke it
String args[] = {"arg1", "arg2"}; // args to pass to the main method, it can be null
method.invoke(null, (Object) args); // first parameter is null because main is static
Maybe you could read classes into the Process with an ObjectInputStream

How to use Apache Commons CLI to parse the property file and --help option?

I have a property file which is like this -
hostName=machineA.domain.host.com
emailFrom=tester#host.com
emailTo=world#host.com
emailCc=hello#host.com
And now I am reading the above property file from my Java program as shown below. I am parsing the above property file manual way as of now -
public class FileReaderTask {
private static String hostName;
private static String emailFrom;
private static String emailTo;
private static String emailCc;
private static final String configFileName = "config.properties";
private static final Properties prop = new Properties();
public static void main(String[] args) {
readConfig(arguments);
// use the above variables here
System.out.println(hostName);
System.out.println(emailFrom);
System.out.println(emailTo);
System.out.println(emailCc);
}
private static void readConfig(String[] args) throws FileNotFoundException, IOException {
if (!TestUtils.isEmpty(args) && args.length != 0) {
prop.load(new FileInputStream(args[0]));
} else {
prop.load(FileReaderTask.class.getClassLoader().getResourceAsStream(configFileName));
}
StringBuilder sb = new StringBuilder();
for (String arg : args) {
sb.append(arg).append("\n");
}
String commandlineProperties = sb.toString();
if (!commandlineProperties.isEmpty()) {
// read, and overwrite, properties from the commandline...
prop.load(new StringReader(commandlineProperties));
}
hostName = prop.getProperty("hostName").trim();
emailFrom = prop.getProperty("emailFrom").trim();
emailTo = prop.getProperty("emailTo").trim();
emailCc = prop.getProperty("emailCc").trim();
}
}
Most of the time, I will be running my above program through command line as a runnable jar like this -
java -jar abc.jar config.properties
java -jar abc.jar config.properties hostName=machineB.domain.host.com
My question is-
Is there any way to add --help option while running the abc.jar that can tell us more about how to run the jar file and what does each property means and how to use them? I have seen --help while running most of the C++ executable or Unix stuff so not sure how we can do the same thing in Java?
Do I need to use CommandLine parser like Commons CLI for this in Java to achieve this and instead of doing manual parsing, I should use Commons CLI to parse the file as well? If yes, then can anyone provide an example how would I do that in my scenario?
In the long run if you plan to add other options in the future then commons-cli is surely a fairly good fit as it makes it easy to add new options and manual parsing quickly becomes complicated.
Take a look at the official examples, they provide a good overview of what the library can do.
Your specific case would probably lead to something like the following:
// create Options object
Options options = new Options();
Option help = new Option( "h", "help", false, "print this message" );
options.addOption(help);
CommandLineParser parser = new PosixParser();
CommandLine cmd = parser.parse( options, args);
if(cmd.hasOption("help") || cmd.getArgList().isEmpty()) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "cli-test [options] <property-file>", options );
return;
}
// do your thing...
System.out.println("Had properties " + cmd.getArgList());

Is there any way to transfer system properties to sub-processes?

I'm writing a program that forms a new sub-process in a following pattern:
proc = java.lang.Runtime.getRuntime().exec("java -jar Xxx.jar");
Though the environment variables are automatically inherited to sub-processes, I think the system properties defined by -D<name of property>=<value of the property> are not.
My question is, if there is any way to transfer the system properties programmatically. Any comments or answers are welcomed. Thanks.
One solution that I've come up with is to define a set of properties to pass to subprocesses, and create a -D<key>=<value> strings from it.
static String[] properties_to_pass = {
"log4j.configuration"
};
Above is the set of system properties to pass. Then...
StringBuffer properties = new StringBuffer();
for ( String property : properties_to_pass ) {
String value = System.getProperty(property);
if ( value != null ) {
String r = String.format("-D%s=%s ", property, value);
properties.append( r );
}
}
And after the above ...
String command_arg = properties.toString();
String command = String.format("java %s -jar Torpedo.jar", command_arg);
java.lang.Runtime.getRuntime.exec( command );
Very naive solution, but works anyway. But still not sure that there might be a better solution. Any further comments are welcomed.
If you are using Java 1.5+ it is highly recommended to use ProcessBuilder to create processes as it allows a lot of convenience methods to achieve what you are trying.
You can achieve sharing of System Properties with ProcessBuilder as following.
Properties props = System.getProperties();
ProcessBuilder builder = new ProcessBuilder(new String[] {"java", "-jar","Xxx.jar"});
Map<String, String> env = builder.environment();
for(String prop:props.stringPropertyNames()) {
env.put(prop, props.getProperty(prop));
}
Process process = builder.start();
With this code from your Child Process you can access any System Properties you pass using -D in your Parent Process using System.getProperty method.

Categories