| | |
returning from command prompt to code
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
Hi All,
I am using the following codeline to run a cmd from cmd prompt.
Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport....
I have my structure like
Class A
B importcmd = new B();
importcmd.startimp();
sysout("out of cmd prompt");
Class B
startimport()
{
Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport....
sysout("here is ur output");
}
In this case entire code of class B gets executed .i.e.the statements after i fire exec cmd
But in Class A nothing gets executed once i call this class...
Can anybody help me out of this..
Thanks in advance
I am using the following codeline to run a cmd from cmd prompt.
Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport....
I have my structure like
Class A
B importcmd = new B();
importcmd.startimp();
sysout("out of cmd prompt");
Class B
startimport()
{
Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport....
sysout("here is ur output");
}
In this case entire code of class B gets executed .i.e.the statements after i fire exec cmd
But in Class A nothing gets executed once i call this class...
Can anybody help me out of this..
Thanks in advance
I'm not 100% sure, but I think this issue could have to do with threads. I think what is happening is the cmd.exe is being run in the main thread and there is no other thread to run the rest of class A until you quit the cmd.exe. If I am correct, you need to implement a third class like so:
Next you need to modify class B like so:
For more information on Threads, see the sun website.
java Syntax (Toggle Plain Text)
public class C implements Runnable { public void run() { Process p1 = Runtime.getRuntime().exec(... } }
java Syntax (Toggle Plain Text)
startimport() { C runnable = new C(); new Thread( runnable ).start(); sysout("here is output"); }
For more information on Threads, see the sun website.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend. •
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
I am going to convert the entire program into batch file including these steps
Anywayz my problem is partially solved
My code was like
In this code when i commented while(s = stdInput.readLine()) the prog started working
But now the problem is two command prompts are strated simultaneously for the same file.
I mean i am calling this class( Call_From_Import) from other class in a loop in which values in zip file are modified...If i use system.exit or p1.waitfor or p1.destroy only 1 cmd prompt is started

Anywayz my problem is partially solved
My code was like
Java Syntax (Toggle Plain Text)
package com.WCM.Vignette; import java.io.*; public class Import_In_VCM { /** * @param args * @throws WCM_Exceptions * @throws * */ void Call_From_Import() throws WCM_Exceptions { File dir1 = new File("C:\\Path"); String s = null; try { //start the command prompt String[] cmds = {"cmd.exe /k start dir"}; int i; for(i=0;i<cmds.length;i++) { System.out.println(cmds[i]); } /* * run the vgnimport command */ Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport -u user -p pwd -h machine:port -s D:\\Datanew12.zip",null,dir1); // read the standard output of the command BufferedReader stdInput = new BufferedReader(new InputStreamReader(p1.getInputStream())); //BufferedReader stdError = new BufferedReader(new // InputStreamReader(p1.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); /*while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } catch (IOException e) { System.out.println("hi u r in catch"); throw new WCM_Exceptions("Exception occurred "); } } }
In this code when i commented while(s = stdInput.readLine()) the prog started working
But now the problem is two command prompts are strated simultaneously for the same file.
I mean i am calling this class( Call_From_Import) from other class in a loop in which values in zip file are modified...If i use system.exit or p1.waitfor or p1.destroy only 1 cmd prompt is started
Last edited by newbie_xml; Mar 6th, 2008 at 12:28 am.
•
•
Join Date: Mar 2008
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
I am going to convert the entire program into batch file including these steps
Anywayz my problem is partially solved
My code was like
Java Syntax (Toggle Plain Text)
package com.WCM.Vignette; import java.io.*; public class Import_In_VCM { /** * @param args * @throws WCM_Exceptions * @throws * */ void Call_From_Import() throws WCM_Exceptions { File dir1 = new File("C:\\Path"); String s = null; try { //start the command prompt String[] cmds = {"cmd.exe /k start dir"}; int i; for(i=0;i<cmds.length;i++) { System.out.println(cmds[i]); } /* * run the vgnimport command */ Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport -u user -p pwd -h machine:port -s D:\\Datanew12.zip",null,dir1); // read the standard output of the command BufferedReader stdInput = new BufferedReader(new InputStreamReader(p1.getInputStream())); //BufferedReader stdError = new BufferedReader(new // InputStreamReader(p1.getErrorStream())); // read the output from the command System.out.println("Here is the standard output of the command:\n"); /*while ((s = stdInput.readLine()) != null) { System.out.println(s); } // read any errors from the attempted command System.out.println("Here is the standard error of the command (if any):\n"); while ((s = stdError.readLine()) != null) { System.out.println(s); } catch (IOException e) { System.out.println("hi u r in catch"); throw new WCM_Exceptions("Exception occurred "); } } }
In this code when i commented while(s = stdInput.readLine()) the prog started working
But now the problem is two command prompts are strated simultaneously for the same file.
I mean i am calling this class( Call_From_Import) from other class in a loop in which values in zip file are modified...If i use system.exit or p1.waitfor or p1.destroy only 1 cmd prompt is started
![]() |
Similar Threads
- Urgent: Postfix Interpreter (Pascal and Delphi)
Other Threads in the Java Forum
- Previous Thread: eclipse breakpoints problem
- Next Thread: Help required for Sun Certified Java Developer Exam(SCJD)
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth businessintelligence chat class classes client code component database desktop draw ebook eclipse encode equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop mac main map method methods mobile netbeans newbie number online open-source oracle parameter print problem program programming project properties recursion reference replaysolutions rotatetext scanner score screen scrollbar server set size sms socket sort sql string superclass swing template test threads time tree windows working xstream





