bullyellis
asked on
Lynx, Java, & Runtime.getRuntime().exec()
LYnx is a text based browser. It can convert .html files to its rough equivalent in text.
I am trying to use a simple java program to call lynx.bat or lynx.exe .
Lynx.bat is located in C:\Lynx\lynx.bat
I want to use the "-dump" parameter.
Then I must put the name of the file "mydoc.htm"
Then the name of the new file "mydoctxt.txt"
So in completion it would look like this "lynx.bat -dump mydoc.htm > mydoctxt.txt"
This works completely fine from the command line, but when I try to wrap it in a simple java program, it fails.
Here is what I have so far...
package javaapplication2;
import java.io.*;
class Main{
public static void main(String argv[]) {
try {
String ls_str;
//Process ls_proc = Runtime.getRuntime().exec( "cmd.exe /E :1900 C:\\lynx\\lynx.exe -dump C:\\test.htm > C:\\test6.txt");
Process ls_proc = Runtime.getRuntime().exec( "C:\\lynx\ \lynx.bat -dump C:\\test.htm > C:\\test5.txt");
DataInputStream ls_in = new DataInputStream(
ls_proc.getInputStream());
try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str) ;
}
} catch (IOException e) {
System.out.println("1st catch block");
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.out.println("2nd catch block");
System.exit(1);
}
System.exit(0);
}
}
When I run that a blank text file called test5.txt is created. It is 0kb in length. If I run it straight from the command line in windows XP though, test5.txt is created successfully. If I uncomment out Process ls_proc = Runtime.getRuntime().exec( "cmd.exe /E :1900 C:\\lynx\\lynx.exe -dump C:\\test.htm > C:\\test6.txt"); and comment out the other line, the program lags and nothing happens.
I know I have to be missing something simple here, because I whipped up the equivalent prog in VB, in like 3 minutes.
I am trying to use a simple java program to call lynx.bat or lynx.exe .
Lynx.bat is located in C:\Lynx\lynx.bat
I want to use the "-dump" parameter.
Then I must put the name of the file "mydoc.htm"
Then the name of the new file "mydoctxt.txt"
So in completion it would look like this "lynx.bat -dump mydoc.htm > mydoctxt.txt"
This works completely fine from the command line, but when I try to wrap it in a simple java program, it fails.
Here is what I have so far...
package javaapplication2;
import java.io.*;
class Main{
public static void main(String argv[]) {
try {
String ls_str;
//Process ls_proc = Runtime.getRuntime().exec(
Process ls_proc = Runtime.getRuntime().exec(
DataInputStream ls_in = new DataInputStream(
ls_proc.getInputStream());
try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str)
}
} catch (IOException e) {
System.out.println("1st catch block");
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.out.println("2nd catch block");
System.exit(1);
}
System.exit(0);
}
}
When I run that a blank text file called test5.txt is created. It is 0kb in length. If I run it straight from the command line in windows XP though, test5.txt is created successfully. If I uncomment out Process ls_proc = Runtime.getRuntime().exec(
I know I have to be missing something simple here, because I whipped up the equivalent prog in VB, in like 3 minutes.
Sure it isn't a matter of (back) slashes.
Did you try these already?
Process ls_proc = Runtime.getRuntime().exec( "cmd.exe /E :1900 C:/lynx/lynx.exe -dump C:/test.htm > C:/test6.txt");
or
Process ls_proc = Runtime.getRuntime().exec( "C:/lynx/l ynx.bat -dump C:/test.htm > C:/test5.txt");
Did you try these already?
Process ls_proc = Runtime.getRuntime().exec(
or
Process ls_proc = Runtime.getRuntime().exec(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here an examples of handing the output from a Process that you should be able to easily adapt to your requirements.
http://www.objects.com.au/java/examples/util/ConsoleExec.do
http://www.objects.com.au/java/examples/util/ConsoleExec.do
ASKER
WebStorm,
Your code works kind of.
The problem I ran into was
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec( "C:\\lynx\ \lynx.bat -dump C:\\test.htm > C:\\test5.txt"),
System.in,false,
new FileOutputStream("C:\\test 5.txt"),tr ue, // stdout redirection
System.err,false);
The 2 instances of "test5.txt" can not be named the same thing.
I get an error that "The process cannot access the file because it is being used by another process." And again I get the empty text file.
However if I change the code to
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec( "C:\\lynx\ \lynx.bat -dump C:\\test.htm > C:\\testa.txt"),
System.in,false,
new FileOutputStream("C:\\test b.txt"),tr ue, // stdout redirection
System.err,false);
Then I get both testa and testb. testa.txt works correctly and is what I want. testb.txt however is another empty text file.
Could you explain this ?
Your code works kind of.
The problem I ran into was
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec(
System.in,false,
new FileOutputStream("C:\\test
System.err,false);
The 2 instances of "test5.txt" can not be named the same thing.
I get an error that "The process cannot access the file because it is being used by another process." And again I get the empty text file.
However if I change the code to
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec(
System.in,false,
new FileOutputStream("C:\\test
System.err,false);
Then I get both testa and testb. testa.txt works correctly and is what I want. testb.txt however is another empty text file.
Could you explain this ?
bullyellis, thanks for the points
When you specify a redirection with '>', cmd.exe will redirect output to this file. So, nothing will be written to the process's stdout stream, and you get an empty file.
If you specify a FileOutputStream you don't have to specify another redirection in the command line and it's only usable with cmd.exe :
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec( "C:\\lynx\ \lynx.bat -dump C:\\test.htm"),
System.in,false,
new FileOutputStream("C:\\test a.txt"),tr ue, // stdout redirection
System.err,false);
Or
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec( "C:\\lynx\ \lynx.bat -dump C:\\test.htm > C:\\testa.txt"),
System.in,false,
System.out,false,
System.err,false);
When you specify a redirection with '>', cmd.exe will redirect output to this file. So, nothing will be written to the process's stdout stream, and you get an empty file.
If you specify a FileOutputStream you don't have to specify another redirection in the command line and it's only usable with cmd.exe :
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec(
System.in,false,
new FileOutputStream("C:\\test
System.err,false);
Or
RunProcess ls_proc = new RunProcess(
Runtime.getRuntime().exec(
System.in,false,
System.out,false,
System.err,false);
so String[] cmdarray = {"C:\\lynx\\lynx.bat","-du
Runtime.getRuntime().exec(
I think I remeber this solving it. Hope I'm right.
Geoff