returning from command prompt to code

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 7
Reputation: newbie_xml is an unknown quantity at this point 
Solved Threads: 0
newbie_xml newbie_xml is offline Offline
Newbie Poster

returning from command prompt to code

 
0
  #1
Mar 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 802
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: returning from command prompt to code

 
0
  #2
Mar 5th, 2008
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:
  1. public class C implements Runnable
  2. {
  3. public void run()
  4. {
  5. Process p1 = Runtime.getRuntime().exec(...
  6. }
  7. }
Next you need to modify class B like so:
  1. startimport()
  2. {
  3. C runnable = new C();
  4. new Thread( runnable ).start();
  5. sysout("here is output");
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: newbie_xml is an unknown quantity at this point 
Solved Threads: 0
newbie_xml newbie_xml is offline Offline
Newbie Poster

Re: returning from command prompt to code

 
0
  #3
Mar 5th, 2008
Thanks a lot...i will implemnt this...but what i think is if i loop throgh the cmd thosannds the threads might create a issue...
Is there some other way out?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 802
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: returning from command prompt to code

 
0
  #4
Mar 5th, 2008
Are you saying you want thousands of command prompts? What exactly are you trying to do in the exec line?
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: newbie_xml is an unknown quantity at this point 
Solved Threads: 0
newbie_xml newbie_xml is offline Offline
Newbie Poster

Re: returning from command prompt to code

 
0
  #5
Mar 5th, 2008
i am logging into the content management system throgh the command and import data into it.So i think doing it through threads could create an issue
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: returning from command prompt to code

 
0
  #6
Mar 5th, 2008
Then why don't you just write a batch or shell script?
A Perfect World
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: newbie_xml is an unknown quantity at this point 
Solved Threads: 0
newbie_xml newbie_xml is offline Offline
Newbie Poster

Re: returning from command prompt to code

 
0
  #7
Mar 6th, 2008
Originally Posted by orko View Post
Then why don't you just write a batch or shell script?
I am going to convert the entire program into batch file including these steps
Anywayz my problem is partially solved
My code was like
  1. package com.WCM.Vignette;
  2.  
  3. import java.io.*;
  4.  
  5.  
  6. public class Import_In_VCM {
  7.  
  8. /**
  9. * @param args
  10. * @throws WCM_Exceptions
  11. * @throws
  12. *
  13. */
  14. void Call_From_Import() throws WCM_Exceptions {
  15. File dir1 = new File("C:\\Path");
  16. String s = null;
  17. try {
  18. //start the command prompt
  19. String[] cmds = {"cmd.exe /k start dir"};
  20. int i;
  21. for(i=0;i<cmds.length;i++)
  22. {
  23. System.out.println(cmds[i]);
  24. }
  25.  
  26. /*
  27. * run the vgnimport command
  28. */
  29.  
  30. Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport -u user -p pwd -h machine:port -s D:\\Datanew12.zip",null,dir1);
  31. // read the standard output of the command
  32. BufferedReader stdInput = new BufferedReader(new
  33. InputStreamReader(p1.getInputStream()));
  34. //BufferedReader stdError = new BufferedReader(new
  35. // InputStreamReader(p1.getErrorStream()));
  36.  
  37. // read the output from the command
  38. System.out.println("Here is the standard output of the command:\n");
  39. /*while ((s = stdInput.readLine()) != null) {
  40. System.out.println(s);
  41. }
  42.  
  43. // read any errors from the attempted command
  44. System.out.println("Here is the standard error of the command (if any):\n");
  45. while ((s = stdError.readLine()) != null) {
  46. System.out.println(s);
  47. }
  48.  
  49.  
  50. catch (IOException e) {
  51. System.out.println("hi u r in catch");
  52. throw new WCM_Exceptions("Exception occurred ");
  53.  
  54.  
  55. }
  56.  
  57.  
  58. }
  59.  
  60. }
  61.  

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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 7
Reputation: newbie_xml is an unknown quantity at this point 
Solved Threads: 0
newbie_xml newbie_xml is offline Offline
Newbie Poster

Re: returning from command prompt to code

 
0
  #8
Mar 6th, 2008
Originally Posted by newbie_xml View Post
I am going to convert the entire program into batch file including these steps
Anywayz my problem is partially solved
My code was like
  1. package com.WCM.Vignette;
  2.  
  3. import java.io.*;
  4.  
  5.  
  6. public class Import_In_VCM {
  7.  
  8. /**
  9. * @param args
  10. * @throws WCM_Exceptions
  11. * @throws
  12. *
  13. */
  14. void Call_From_Import() throws WCM_Exceptions {
  15. File dir1 = new File("C:\\Path");
  16. String s = null;
  17. try {
  18. //start the command prompt
  19. String[] cmds = {"cmd.exe /k start dir"};
  20. int i;
  21. for(i=0;i<cmds.length;i++)
  22. {
  23. System.out.println(cmds[i]);
  24. }
  25.  
  26. /*
  27. * run the vgnimport command
  28. */
  29.  
  30. Process p1 = Runtime.getRuntime().exec("cmd.exe /k start vgnimport -u user -p pwd -h machine:port -s D:\\Datanew12.zip",null,dir1);
  31. // read the standard output of the command
  32. BufferedReader stdInput = new BufferedReader(new
  33. InputStreamReader(p1.getInputStream()));
  34. //BufferedReader stdError = new BufferedReader(new
  35. // InputStreamReader(p1.getErrorStream()));
  36.  
  37. // read the output from the command
  38. System.out.println("Here is the standard output of the command:\n");
  39. /*while ((s = stdInput.readLine()) != null) {
  40. System.out.println(s);
  41. }
  42.  
  43. // read any errors from the attempted command
  44. System.out.println("Here is the standard error of the command (if any):\n");
  45. while ((s = stdError.readLine()) != null) {
  46. System.out.println(s);
  47. }
  48.  
  49.  
  50. catch (IOException e) {
  51. System.out.println("hi u r in catch");
  52. throw new WCM_Exceptions("Exception occurred ");
  53.  
  54.  
  55. }
  56.  
  57.  
  58. }
  59.  
  60. }
  61.  

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
Please help me in solving the problem....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC