ProcessBuilder - make call to systeminfo | find xxx

Hi,

the question is if I can make processbuilder execute

systeminfo | find"Total Physical Memory"

instead of parsing the output given from

ProcessBuilder processBuilder =new ProcessBuilder("systeminfo");

The application I am working on is for personal useonly so I don't have to think about multi-lingual stuff or so, even if that would be preferable. The plan is just to get it to work, without JNI or bundled platform-specific executables.

- Adam

edit:

I thought this would work but it's not.

ProcessBuilder processBuilder =new ProcessBuilder("systeminfo | find \"Total Physical Memory:\"");

try{

Process process = processBuilder.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);

}

}

catch(Exception e){

e.printStackTrace();

}

[1563 byte] By [Ashama] at [2007-11-27 6:15:02]
# 1

Adam,

Sounds like java's probably not the best language for the job... I suspect you'd be better off using perl if you're going to be doing a lot of parsing of the output from external commands.

try this approach instead?

http://forum.java.sun.com/thread.jspa?threadID=179367&messageID=1230999

corlettka at 2007-7-12 17:25:05 > top of Java-index,Java Essentials,Java Programming...
# 2

this works... Me thinks it was a path problem thingummy... and it doesn't like the pipe for some reason... I'd never tried it before.package forums;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class ProcessBuilderExample {

public static void main(String[] args) {

BufferedReader br = null;

try {

ProcessBuilder processBuilder = new ProcessBuilder("C:\\WINDOWS\\system32\\systeminfo");

Process process = processBuilder.start();

br = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;

while ((line = br.readLine()) != null) {

if (line.matches(".*Total Physical Memory.*")) {

System.out.println(line);

}

}

} catch(Exception e){

e.printStackTrace();

} finally {

try{br.close();}catch(Exception ignore){}

}

}

}

Cheers. Keith.

corlettka at 2007-7-12 17:25:05 > top of Java-index,Java Essentials,Java Programming...