The Artima Developer Community
Sponsored Link

Java Answers Forum
running a program from a java application

3 replies on 1 page. Most recent reply: Feb 27, 2002 3:42 AM by Akhil

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
chan

Posts: 2
Nickname: champion
Registered: Feb, 2002

running a program from a java application Posted: Feb 25, 2002 2:31 PM
Reply to this message Reply
Advertisement
I wrote this small program to run the date command within the java application.
import java.lang.*;
import java.io.*;
class rundate {
public static void main(String[] args) throws IOException {
String dateCommand = "date";
rundate champ = new rundate();
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(dateCommand);

}
}

This compiles and runs. Now is this suposed to actually run the date command so i see it
on the screen? Because there is no output on the screen? If so please help me out, or tell me how I could. Thanks


Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: running a program from a java application Posted: Feb 25, 2002 3:12 PM
Reply to this message Reply
The output of the process is written back to your Java app. You can grab it via the Process object's getInputStream method, then print it to the standard output yourself. Something like:
Process proc = rt.exec(dateCommand);
bis = new BufferedInputStream(proc.getInputStream());


Then just read from bis and write to the standard output.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: running a program from a java application Posted: Feb 25, 2002 11:55 PM
Reply to this message Reply
Actually, assuming you are running Windows/DOS, I think the problem is that date is not program, but an internal command of the command interpreter (command.com, or cmd.exe depending on your Windows platform -- look at the COMSPEC environment variable).

You could instead try running "command /c echo.|date" instead, or create a batch file and call that. The reason I added the "echo.|" part is that date will sit there and wait for you to enter a new date without it.

Akhil

Posts: 3
Nickname: akhilnagpa
Registered: Feb, 2002

Re: running a program from a java application Posted: Feb 27, 2002 3:42 AM
Reply to this message Reply
I am posting a program i wrote sometime back..i think it can help you.
///////////////////////////////////////////////////////
import java.io.*;
public class Test
{
public static void main(String args[])
{
try
{

//Process p = Runtime.getRuntime().exec("D:\\Akhil-Kiwi\\Kiwi2.5-31may\\kiwiClient\\runclient .bat");
Process p = Runtime.getRuntime().exec("E:\\Apache Tomcat 4.0\\bin\\startup.bat");
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
bw.write("1");
bw.flush();
String line;
while ((line = br.readLine()) != null)
{
System.out.println(line);
}
br.close();
bw.close();

System.out.println(p.exitValue());




}catch(Exception e)
{
System.out.println("exception Occured");
e.printStackTrace();
}
}
}

Flat View: This topic has 3 replies on 1 page
Topic: creating a book out of a panel for printing Previous Topic   Next Topic Topic: chatting program

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use