Run a jar file out of java programm with many system properties - java

I want to start a .jar File out of my java program using the Process Builder like this:
ProcessBuilder pb = new ProcessBuilder("java", "-Xdebug", "-DpropKey1=value", "-DpropKey2=value", "MyJar.jar");
Process p = pb.start();
I am looking for a smart way to pass a high amount of the system properties my java program is using to the ProcessBuilder. Right at this moment I am doing it like this:
StringBuilder d1 = new StringBuilder(100);
d1.append("-DpropKey1=");
d1.append(System.getProperty("propKey1"));
String d1Str = d1.toString();
StringBuilder d2 = new StringBuilder(100);
d2.append("-DpropKey2=");
d2.append(System.getProperty("propKey2"));
String d2Str = d2.toString();
ProcessBuilder pb = new ProcessBuilder("java", "-Xdebug", d1Str, d2Str, "MyJar.jar");
Process p = pb.start();
But this way doesn't seem very smart to me. It's just I have a lot of system properties I want to pass out of the java programm (more than 10). It doesn't feel right to use a StringBuilder for every system property I want to pass.

Try this:
String[] params=...
new ProcessBuilder(params).start()
Params is an array of parameters as String.
Old:
Write a method, which does the trick. As argument it could get a Map<String, String>.
The key of an entry is the parameter name, and the value of an entry is the parameter value.
The method builds the String in a loop, which is iterating over the entry set of the map.
Sorry, I read false! This will not work this way with processbuilder!

Related

How to pass different values in a StringBuilder as separate parameter with a Process call for executing python script from Java

I am executing a python script from Java using Process approach. I have this code part where I have a StringBuilder with following structure:(val1,val2,...).
Now I have this part of code which uses Process approach to execute the python scripts from within Java code:
ProcessBuilder pb = new ProcessBuilder("python","test1.py");
Process p = pb.start();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int ret = new Integer(in.readLine()).intValue();
Now what I want is to first check if my StringBuilder str is empty or not i.e. the string is just () and there is nothing inside () in str. If it is not empty (i.e. there are values inside ()) then I need to pass each value as a separate parameter with the Process call for my python script. For example if my str is (val1,val2) then I need to pass val1 and val2 as a separate parameters in the Process call for executing the python script from within Java code.
How can I modify my current code to include this?
Why use a StringBuilder at all instead of just passing your arguments along?
new ProcessBuilder("python", "test1.py", "val1", "val2")
You can dynamically create that parameter array if necessary.
List<String> args = new ArrayList<>();
args.add("python");
args.add("test1.py");
args.addAll(myOtherArgsThatICollectedInAList);
new ProcessBuilder(args.toArray(new String[0]);

Program execution in java

I am trying to open an exe file, specificly the IndriRunQuery.exe which is one of the tools that offers the Lemur Indri package. When i use the command prompt i write the following command:
IndriRunQuery Queries.txt
With this, the editting of the queries that are included in Queries.txt (which is passed as a parameter in the above command) is starting.
Then after a descent amount of time has passed ,i write the following in order to save the results that are produced in a file named Results.txt:
IndriRunQuery Queries.txt >Results.txt
My problem is that every time that i want to edit a file which contains queries
i need to do the same steps. i have 20 different query files to edit. I am trying to find a way to do it by using a java program but i can not figure it out.
I have used these lines of code but it doesnot work at all.
Can anyone help me out with this?
ProcessBuilder builder = new ProcessBuilder("C:\\Program Files\\Indri\\Indri 5.8\\bin\\IndriRunQuery.exe",
"C:\\Users\\Πετρής\\Desktop\\TitlesRel.txt");
builder.start();
ProcessBuilder builder2 = new ProcessBuilder("C:\\Program Files\\Indri\\Indri 5.8\\bin\\IndriRunQuery.exe",
"C:\\Users\\Πετρής\\Desktop\\TitlesRel.txt",">C:\\Users\\Πετρής\\Desktop\\resultsexample3.txt");
builder2.start();
The correct syntax is as below:
// Create ProcessBuilder.
ProcessBuilder p = new ProcessBuilder();
// Use command "notepad.exe" and open the file.
p.command("notepad.exe", "C:\\file.txt");
p.start();
Or
Process p = Runtime.getRuntime().exec("cmd /c start " + file.getAbsolutePath());

Java library for finding executables

I am using the the ProcessBuilder class to execute executables on Windows and Linux.
Is there an easy way to find these executables without knowing the directory path to the executable.
e.g.
//which command functionality
String executable = which("executable_name");
List<String> command = new ArrayList<String>();
command.add(executable);
ProcessBuilder builder = new ProcessBuilder(command);
..
..
It would be great if there was a function like the which command on linux?
Any ideas or will I have to loop over and parse the PATH environment variables using the
System.getenv("PATH");
Use the where command on Windows.
WHERE [/R dir] [/Q] [/F] [/T] pattern
If you do not specify a search directory using /R, it searches the current directory and in the paths specified by the PATH environment variable. Here's a sample code that finds the two locations where notepad.exe resides on Windows.
String searchCmd;
if (System.getProperty("os.name").contains("Windows")) {
searchCmd = "where";
} else { // I'm assuming Linux here
searchCmd = "which";
}
ProcessBuilder procBuilder = new ProcessBuilder(searchCmd, "notepad.exe");
Process process = procBuilder.start();
ArrayList<String> filePaths = new ArrayList<String>();
Scanner scanner = new Scanner(process.getInputStream());
while (scanner.hasNextLine()) {
filePaths.add(scanner.nextLine());
}
scanner.close();
System.out.println(filePaths);
Output:
[C:\Windows\System32\notepad.exe, C:\Windows\notepad.exe]
Note: I've only tested this on Windows. You may have to modify (probably the command options and the way you parse which output) to make it work on Linux.

How to write command in process Builder

I am Running shell script using cygwin and java.
ProcessBuilder pb =new ProcessBuilder
("sh", "app.sh", "ib2", "12", "11", "AK-RD", "02.20", "D:\\cygwin\\bin\\test\\delta");
My script is running when i am hard coding parameters. I want to pass these parameters through text box values.
How to do this.
String cmmd[] = new String[8];
cmmd[0] ="\"sh\"";
cmmd[1] ="\"app.sh\"";
cmmd[2] ="\""+txt_threeltr.getText()+"\"";
cmmd[3] ="\""+txt_month_c.getText()+"\"";
cmmd[4] ="\""+txt_year_C.getText()+"\"";
cmmd[5] ="\""+txt_partNumber.getText()+"\"";
cmmd[6] ="\""+txt_version.getText()+"\"";
cmmd[7] ="\""+txt_destinationname.getText()+"\"";
ProcessBuilder pb =new ProcessBuilder(Arrays.toString(cmmd));
Or is there any other way to do this.
Since ProcessBuilder has a varargs string constructor, you can do this:
ProcessBuilder pb = new ProcessBuilder(cmmd);
Alternatively, don't construct an array. Create it like this:
ProcessBuilder pb = new ProcessBuilder ("sh",
"app.sh",
txt_threeltr.getText(),
txt_month_c.getText(),
txt_year_C.getText(),
txt_partNumber.getText(),
txt_version.getText(),
txt_destinationname.getText());
The ProcessBuilder has a vargs constructor that you can pass your array to. Pass the values exactly as input from the text boxes (no quotes), and it will take care of any necessary escaping for you.

running sipp from java

I am programming a Java interface for the sipp command line program. My current code is:
ProcessBuilder builder = new ProcessBuilder("sipp", "-sn uac",
"127.0.0.1");
Map<String, String> environment = builder.environment();
Process javap = builder.start();
InputStreamReader tempReader = new InputStreamReader(new BufferedInputStream(javap.getInputStream()));
BufferedReader reader = new BufferedReader(tempReader);
while (true){
String line = reader.readLine();
if (line == null)
break;
System.out.println(line);
}
This does not work for me thought, I have sipp environment variable set so this is not the problem. The standard output is sipp's help message. What am I doing wrong? Also I would like to know once I got sipp running is it possible to pass arguments to the processBuilder object associated with it so I can change the call rate? i.e. sipp let users change call rate by pressing + , - , * is this possible?
Try breaking up the -sn and uac parameters:
ProcessBuilder builder = new ProcessBuilder("sipp", "-sn", "uac", "127.0.0.1");
Also I would like to know once I got
sipp running is it possible to pass
arguments to the processBuilder object
associated with it so I can change the
call rate?
If sipp is expecting input from standard in, you should be able to grab an output stream (javap.getOutputStream()) to the process and write commands to it. I don't know anything about sipp to tell you whether that's how it works, though.

Categories

Resources