[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

calling system commands within Java

Hi,
Can we directly call a system command within java code?

Here is an example:

grep '^(SdD)' args[2]; 

Open in new window


args[2] is the path to a text file.

Thanks,

0
Tolgar
Asked:
Tolgar
  • 10
  • 7
  • 4
2 Solutions
 
for_yanCommented:
Yes we can - you should use

Runtime.getRuntime().exec("command arguments");
0
 
for_yanCommented:
0
 
for_yanCommented:

And this is how you read the ouput from the second example (importnat part of the process):
http://www.exampledepot.com/egs/java.lang/ReadFromCommand.html
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CEHJCommented:
You'd be better off calling a shell script. But see my comments here http:Q__26889988.html
Process p = Runtime.getRuntime().exec(new String[] { "sh mygrep.sh", args[2] });

Open in new window

0
 
TolgarAuthor Commented:
So as a result is this correct?

java.lang.String myFiles = Runtime.getRuntime().exec(grep '^(SdD)' args[3]);

Open in new window



Thanks,
0
 
CEHJCommented:
Sorry that should have been
Process p = Runtime.getRuntime().exec(new String[] { "sh", "mygrep.sh", args[2] });

Open in new window

0
 
CEHJCommented:
With a shell script consisting of

grep '^(SdD)' "${@}"

I've just tested the following code and it works fine.

You can grep in files in pure Java too
public class GrepTest {
    public static void main(String[] args) throws Exception {
	Process p = Runtime.getRuntime().exec(new String[] { "sh",  "mygrep.sh", args[2] });

	StringBuilder stderr = new StringBuilder();
	StringBuilder stdout = new StringBuilder();
	net.proteanit.io.IOUtils.collectProcessStreams(p, stderr, stdout);
	System.out.println(stdout);
    }
}

Open in new window

0
 
TolgarAuthor Commented:
So is this gonna work?

java.lang.String myFiles = Runtime.getRuntime().exec(grep '^(SdD)' args[3]);

Open in new window



Thanks,
0
 
for_yanCommented:
No, one thing
you need to havwe arguments in quotes:

Process p = Runtime.getRuntime().exec("grep '^(SdD)'  " + args[3]);
and then you need to collect the output as CEHJ showed:

      StringBuilder stderr = new StringBuilder();
      StringBuilder stdout = new StringBuilder();
      net.proteanit.io.IOUtils.collectProcessStreams(p, stderr, stdout);
      System.out.println(stdout);

0
 
CEHJCommented:
Let's take a closer look there: i don't know what version of grep you're using, but the effect of that command with the greps i know would effectively either return args[3] or nothing. What is your intention there?
0
 
TolgarAuthor Commented:
So args[3] is a path to a file. What I would like to do is:

grep the lines in this file that starts with s or d or D.

Thanks,
0
 
CEHJCommented:
That would be
grep '^[sdD]' foo

Open in new window

0
 
for_yanCommented:
Maybe it is still easier to do it in Java, rather than going to the system ?
0
 
TolgarAuthor Commented:
You may be right. So how can I make it in Java only without going to the system?

Thanks,
0
 
CEHJCommented:
Just read into a List and add if

s.matches("^[sDd].*");
0
 
for_yanCommented:


DataInputStream in = new DataInputStream(new FileInputStream(args[3]))
String buff = null;
while((buff=in.readLine() )!= null){
buff = in.readLine();
if(buff.startsWith("s") || buff.startsWith("S") || buff.startsWith("D")){
System.out.println(buff);
}
in.close();
}
0
 
for_yanCommented:

Or you can collect it to the String like that:

String s = "";
try{
DataInputStream in = new DataInputStream(new FileInputStream(args[3]))
String buff = null;
while((buff=in.readLine() )!= null){
buff = in.readLine();
if(buff.startsWith("s") || buff.startsWith("S") || buff.startsWith("D")){
System.out.println(buff);
s += buff + System.getProperty("line.separator");
}

}
in.close();
} catch(Exception ex){
ex.printStackTrace();
}
0
 
for_yanCommented:
I guess in the first case I missed one closing brace;
in the second case should be OK
0
 
CEHJCommented:
Try the following (the superclass is at http://technojeeves.com/joomla/index.php/free/74-string-list ) and try it out with a file called 'x.txt'
public class SelectiveStringList extends StringList {

    @Override
    public boolean add(String s) {
	boolean result = s.matches("^[sDd].*");
	if (result) {
	    super.add(s);
	}
	return result;
    }
    public static void main(String[] args) {
	SelectiveStringList matches = new SelectiveStringList();
	matches.read("x.txt");
	for(String s :  matches) {
	    System.out.println(s);
	}
    }
}

Open in new window

0
 
for_yanCommented:
Sorry one readline() too much in my code
0
 
for_yanCommented:
Just remobve first line inside the while loop
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 10
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now