The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with calling a file from Java file(GUI)

4 replies on 1 page. Most recent reply: Aug 29, 2002 11:30 AM by Andy Sufferter

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 4 replies on 1 page
jagunda

Posts: 1
Nickname: jagu
Registered: Jun, 2002

Help with calling a file from Java file(GUI) Posted: Jun 26, 2002 3:38 PM
Reply to this message Reply
Advertisement
HI,
I have program that creates a GUI application.
Now what I want to do is at the end of this program when I click on a button, I want that program to run a batch file.
I am using : try{
String[] cmd = new String[3];
cmd[0] = "cmd.exe ";
cmd[1] = " /c";
cmd[2] = "C:\\javap\\ps.bat" ;

Runtime rt = Runtime.getRuntime();
System.out.println("Batch file: " + cmd[0] + cmd[1] + cmd[2]);
Process p = rt.exec(cmd);

}catch(Exception ioe)
{
System.out.println("error!!!");
}


but it does not seem to do anything. It just runs the rest of the program and exits.
It does not give me any errors or exceptions ..so
Can you please helpe me out.

Jagu


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help with calling a file from Java file(GUI) Posted: Jun 26, 2002 6:00 PM
Reply to this message Reply
This subject has been beaten to death several times before in this forum, but it appears to have some feline properties.

It looks to me like there might be one (or more) of the following problems with your code:
1) You are specifying the command as "cmd.exe " -- with a space. I'm sure no such program exists on your machine, since it is pretty much impossible to have a space character at the end of a file, especially after the extension, on Windows.
2) Depending upon which version of Windows you are running, the command interpreter may be cmd.exe or command.com.
3) You may need to specify the full path of the command to run.
4) Maybe the batch file doesn't exist.
5) A few other things that don't occur to me at the moment, but will right after I click on Post Message, as usual.

Why don't you just use the simple String overloaded version of exec()? Then it would be a simple matter of calling it like so:
   rt.exec("C:\\WINDOWS\\system32\\cmd.exe /c C:\\javap\\ps.bat" );


Of course, I won't even go into the cheesiness of calling a batch file from a Java program. I'll tell you how to release the safety, but it's your fault if you shoot yourself in the foot! :-)

A.V.K.Adinarayana

Posts: 1
Nickname: adi
Registered: Aug, 2002

Re: Help with calling a file from Java file(GUI) Posted: Aug 15, 2002 2:35 AM
Reply to this message Reply
Hi Try this code , you will get error messages also.

//************This code take back up of mdb before run the batchfiles *****************
String filename = "C:\\PricingWorkFlow\\Development\\Database\\runscript1.bat";
System.out.println("Starting");
try{
FileReader fr = new FileReader(filename);
String nextCommand = "";
BufferedReader br = new BufferedReader(fr);
while ((nextCommand = br.readLine()) != null){
String[] commands = {nextCommand};
runCommand(commands);
}
System.out.println("Stopping");
String[] commands = {"exit"};
runCommand(commands);

}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}

//***************************************************************************** ********
/** Runs the commands in a process and displays the results.
*/
public void runCommand(String[] args){
int number = args.length;
try{
String[] commands;

if (System.getProperty("os.name").compareToIgnoreCase("windows") > 0){
commands = new String[number + 2];
commands[0] = "command.com"; //for Windows 95, 98, ME. etc.
if (System.getProperty("os.name").compareToIgnoreCase("nt") > 0){
//commands[0] = "cmd.exe"; //for Windows NT
commands[0] = "command.com"; //for Windows NT
}
commands[1] = "/c";
for (int i = 0; i < number;i++){
commands[i+2] = args;
}
}else{
commands = new String[number];
for (int i = 0; i < number;i++){
commands = args;
}
}

System.out.print("Executing: ");
for (int i = 0; i< commands.length;i++){
System.out.print(commands + " ");
}

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(commands[0] + " " + commands[1] + " " + commands[2]);
// Because some native platforms only provide limited buffer size
// for standard input and output streams, failure to promptly write
// the input stream or read the output stream of the subprocess
// may cause the subprocess to block, and even deadlock.
CheckStream csin = new CheckStream(process.getInputStream());
CheckStream cserr = new CheckStream(process.getErrorStream());
csin.start();
cserr.start();
System.out.println("Waiting for command process to terminate.");
int done = process.waitFor();
process.destroy();
System.out.println("... Done.");
}catch(InterruptedException ie){
System.out.println("InterruptedException: " + ie.getMessage());
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}

/** Inner class for checking the results if any of an InputStream.
*/
class CheckStream extends Thread{

BufferedReader br;
String lineread = "";

/** Constructor needs an InputStream to form an anonymous
* InputStreamReader which is used to create a BufferedReader
* for reading the stream.
*/
CheckStream(InputStream is){
this.br = new BufferedReader(new InputStreamReader(is));
}

/** Reads the input stream and displays anything returned.
*/
public void run(){
try{
while ((lineread = br.readLine()) != null){
System.out.println(lineread);
}
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}
}

sumit

Posts: 1
Nickname: sumitprata
Registered: Aug, 2002

Re: Help with calling a file from Java file(GUI) Posted: Aug 19, 2002 9:54 AM
Reply to this message Reply
This will surely solve ur problem as u r only generating the process object from it.Try to format it into the java output stream.

Try this one !

go to link
http://www.rgagnon.com/javadetails/java-0014.html


Execute an external program
This example will capture the output (from stdio) of an external program. import java.io.*;
public class CmdExec {
public CmdExec(String cmdline) {
try {
String line;
Process p = Runtime.getRuntime().exec(cmdline);
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
}
catch (Exception err) {
err.printStackTrace();
}
}

public static void main(String argv[]) {
new CmdExec("myprog.bat");
}
}




[myprog.bat]
echo hello world!

Andy Sufferter

Posts: 1
Nickname: andy112
Registered: Aug, 2002

Re: Help with calling a file from Java file(GUI) Posted: Aug 29, 2002 11:30 AM
Reply to this message Reply
Even with all there is out there about running dos commands from a java GUI program I still can't get mine to work. I have tried what has been mentioned previously and still no luck!

This is what I have tried to run:
try
{
String line;
p = Runtime.getRuntime().exec("C:\\WINDOWS\\system32\\command.com /c G:\\Research\\GUI\\test.bat");
BufferedReader input = new BufferedReader (new InputStreamReader(p.getInputStream()));
while ((line=input.readLine())!=null)
{
System.out.println(line);
}
input.close();
}

catch (Exception e)
{
e.printStackTrace();
}

and this is the error message I get:
java.io.IOException: CreateProcess: C:\WINDOWS\system32\command.com /c G:\Research\GUI\test.bat error=3

at java.lang.Win32Process.create(Native Method)

at java.lang.Win32Process.<init>(Win32Process.java:64)

at java.lang.Runtime.execInternal(Native Method)

at java.lang.Runtime.exec(Runtime.java:272)

at java.lang.Runtime.exec(Runtime.java:195)

at java.lang.Runtime.exec(Runtime.java:152)

at Test.<init>(Test.java, Compiled Code)

at InputFile.<init>(InputFile.java, Compiled Code)

at Interface.actionPerformed(Interface.java, Compiled Code)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1066)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.j ava:1101)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)

at javax.swing.AbstractButton.doClick(AbstractButton.java:226)

at javax.swing.plaf.basic.BasicMenuItemUI$MouseInputHandler.mouseReleased(BasicMen uItemUI.java:754)

at java.awt.Component.processMouseEvent(Component.java:3166)

at java.awt.Component.processEvent(Component.java, Compiled Code)

at java.awt.Container.processEvent(Container.java, Compiled Code)

at java.awt.Component.dispatchEventImpl(Component.java, Compiled Code)

at java.awt.Container.dispatchEventImpl(Container.java, Compiled Code)

at java.awt.Component.dispatchEvent(Component.java, Compiled Code)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java, Compiled Code)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java, Compiled Code)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java, Compiled Code)

at java.awt.Container.dispatchEventImpl(Container.java, Compiled Code)

at java.awt.Window.dispatchEventImpl(Window.java, Compiled Code)

at java.awt.Component.dispatchEvent(Component.java, Compiled Code)

at java.awt.EventQueue.dispatchEvent(EventQueue.java, Compiled Code)

at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java, Compiled Code)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:92)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:83)

Error

Any help would be greatly appreciated!

Thanks!

Flat View: This topic has 4 replies on 1 page
Topic: MVC architecture Previous Topic   Next Topic Topic: Does SourceForge have problem?

Sponsored Links



Google
  Web Artima.com   

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