I am writing a java code to print current process that is on top of all process.
I write this:-
String process;
Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((process = input.readLine()) != null) {
System.out.println(process);
}
input.close();
it prints all running processes but i want only one process that is on top of all other process and that is currently visible.
You might replace code
while ((process = input.readLine()) != null) {
System.out.println(process);
}
with this,
process = input.readLine();
System.out.println(process);
Related
I am new to java and i am calling a Python script from java using processbuilder and trying read python output in java.
ProcessBuilder pb = new ProcessBuilder(Arrays.asList("python","PyScript.py",""+path));
Process p = pb.start();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
logger.debug("Value of python output is"+line);
System.out.println("in while loop");
}
readline is getting null. when i run through command prompt its running fine.
I'm trying to run Handbrake through a Java app I'm writing, and am having trouble waiting for Handbrake to finish.
When I try this :
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", command);
Process p = builder.start();
BufferedReader inputreader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line = inputreader.readLine()) != null)
{
System.out.println(line);
}
The output I get is :
Encoding: task 1 of 1, 0.00 %
Over and over, and the file never gets converted.
When I change it to the following:
BufferedReader inputreader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader errorreader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = null;
String line2 = null;
while((line = inputreader.readLine()) != null && (line2 = errorreader.readLine()) != null)
{
System.out.println(line);
System.out.println(line2);
}
It works on my test files, however it gets hung-up when the errorreader runs out of lines to read and the readLine() locks the thread infinitely. On full length files the file gets converted but this portion of code gets locked so it never continues with the application.
Any suggestions?
Call builder.redirectErrorStream(true); before creating the process (this will merge the input and the error stream into one: the input stream), and only read from the InputStream.
That should solve the problem of the error stream running out of data before the input stream.
If you do want to keep them separate, then you can start two threads, on to read from the input stream and one from the error stream.
I have an exe file and i want to execute it for a large number of times passing a variable as an input and print the output for each case..
Runtime runtime = Runtime.getRuntime();
for(int i=0;i<1000;i++)
{
Process p = runtime.exec("cmd /c start C:/Users/sbm/workspace/Codex/a.exe",i);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null)
{
System.out.println(line);
}
}
Even if i get the output in a file it will be helpful.
You can do something like this
for(int i=0;i<1000;i++) {
ProcessBuilder builder = new ProcessBuilder("urcmd","urarg");
builder.redirectOutput(new File("C:\\output\\process"+i+".txt"));
builder.start();
}
I'm trying to run /usr/bin/perl -e 'for(my $i=0;$i<1000;$i++){print 1x1000;print STDERR 2x1000;}' (which works in terminal) with my program.
ProcessBuilder pb = new ProcessBuilder(go); //go is the command
process = pb.start();
BufferedReader incommandbuf = new BufferedReader(new InputStreamReader(process.getInputStream()),1024*1000);
BufferedReader errcommandbuf = new BufferedReader(new InputStreamReader(process.getErrorStream()),1024*1000);
stdString = "";
while ((line = incommandbuf.readLine()) != null)
{
stdString += line + "\n";
}
String errorstrtemp = "";
while ((line = errcommandbuf.readLine()) != null)
{
errorstrtemp += line + "\n";
}
If I try to run this it hangs on while ((line = incommandbuf.readLine()) != null). The program runs if I change the command to /usr/bin/perl -e 'for(my $i=0;$i<64;$i++){print 1x1000;print STDERR 2x1000;}'. If it goes up to 65 and higher it doesn't work. At first I thought I just have to change the size of the my BufferedReaders but it didn't help. Any clue on what is causing this? I will provide any additional info if needed.
Thanks.
You are reading one stream at a time. When the other stream fills up the buffer, your Process will stop waiting for you to read it. The solution is to either read the streams in different threads or use ProcessBuilder.redirectErrorStream
Is there a way using Java that I can gain a list of all active processes running on a Mac?
I can do so in Windows using the code below to return the Task List, but that throws an exception on a Mac. I want my app to stop if certain applications are also running.
Any ideas? Thanks.
Windows Code:
Process p = Runtime.getRuntime().exec("tasklist.exe /nh");
BufferedReader input = new BufferedReader
(new InputStreamReader(p.getInputStream()));
//while there are more processes in the task manager list
while ((line = input.readLine()) != null) {
//insert code here for each task running
}
String line;
String sysUserName=System.getProperty("user.name");
Process p = Runtime.getRuntime().exec("tasklist /fi \"username eq"+sysUserName+"\""); // for windows
// Process p = Runtime.getRuntime().exec("ps -u "+sysUserName+""); // for mac
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
}
input.close();
tasklist.exe does not exist on Mac. Use something like ps -eaf