Controlling "CMD.EXE" via Process - Never ends?

I'm trying to write a Java program that parses a text file & executes various command promt commands (I'm trying to keep it platform independant, because I know Lin/Un/nix users who would find this userful as well). I found a small tutorial on the web which gave an example of:
Process p = Runtime.getRuntime().exec("<YOUR COMMAND HERE>");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getInputStream()));
Now; I have inserted "cmd.exe" for <YOUR COMMAND HERE>. If I write a command out to stdOutput, the first command executes, but not subsequent ones. Small segment looks like:
while (line != null) {
stdOutput.write("mkdir \"" + new_folder + "\"\n");
stdOutput.flush();
line = file_reader.readLine();
This is an abridged version of my ode, but thats the problem. I get the first folder created, the process never terminates and I've tried emptying out the stdInput & stdErr bufferes, but it still won't execute subsequent commands issued to stdOutput.
Can someone help me? I'd rather not write this all to a .BAT/.sh file :(

I am quite perplexed now! I have just tried the following code on my PC, and have ended up with the most annoying two folders under my C:\ drive!
import java.io.*;
public class Cmd {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("cmd.exe");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getInputStream()));
//            System.out.println(stdInput.readLine());
//            System.out.println(stdInput.readLine());
//            System.out.println(stdInput.readLine());
            stdOutput.write("mkdir \"c:\\folder1\"\n");
            stdOutput.flush();
//            System.out.println(stdInput.readLine());
            stdOutput.write("mkdir \"c:\\folder2\"\n");
            stdOutput.flush();
//            System.out.println(stdInput.readLine());
        } catch (IOException e) {
            e.printStackTrace();
Perlexion over! Since you had your code in a loop I tried the same. I saw that in a loop creating 10 folders, the code created only two. See below the code that fixed this problem:
import java.io.*;
public class Cmd {
    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("cmd.exe");
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedWriter stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getInputStream()));
            System.out.println(stdInput.readLine());
            System.out.println(stdInput.readLine());
            System.out.println(stdInput.readLine());
            stdOutput.write("mkdir \"c:\\folder1\"\n");
            stdOutput.flush();
            System.out.println(stdInput.readLine());
            stdOutput.write("mkdir \"c:\\folder2\"\n");
            stdOutput.flush();
            System.out.println(stdInput.readLine());
        } catch (IOException e) {
            e.printStackTrace();
}Commenting out different combinations of System.outs gives different results.
In summary: it is important that you match the correct number of output lines from the CMD.EXE output stream with readLines.
I didn't like how the wording came out, but you get the gist.
HTH,
Manuel Amago.

Similar Messages

  • I created a new administrator USER account. I deleted the original one but the process never ends. It's been 3 days now.

    I created a new administrator USER account. I deleted the original one but the process never ends. It's been 3 days now. And I can't close preferences window or turn off the computer.
    It's stays in the "deleting" but I can't cancel it or close the window.
    What can I do?

    Here are two screen shots to show you what I am seeing.  The first screen shot shows it allowing me to select (highlighted in blue) my admin user (which is what I am locced in as).  The second screen shot shows it allowing me to select the "Guest" user (highlighted in blue).  However when I click on the user "Orion" nothing happens.  It will not change to highlight that user.

  • Process never ends

    Hi,
    I'm developing -fixing somebody application- a SuperWaba application. SuperWaba is a poor minor release of Java Runtime. I'm transforming SQL Scripts into PDB files -i hate PDB files.
    I make a Class that extends MainWindow that is called by Waba Applet. Waba Applet extends Java Applet -I hope so. When I run my class from the shell (cmd, over a Windows XP), an Applet Window appears and execute without erros my script transforming a SQL script in one or multiple pdb files (depending number of create sentences).
    But, when i call my script from a PHP Script -i really hate this app, PHP + Java + ASP + Visual Basic ... can you believe it ? - the PHP Script launch Java, but Java app never ends blocking my PHP script. I think there is a problem in Applet Window creation, because there is no window or display to launch the app.
    There is a way to run an Applet in background with no visualization ?
    THX 4 Ur HELP

    i really appreciate your help but i forgot to explain the problem
    First, i decide to create a simple Java App that uses the PDBDriver JAR, but the #~"$%" Jar uses something from the Applet. When I run my class it launch these error.
    java.lang.NullPointerException
            at java.applet.Applet.getCodeBase(Unknown Source)
            at waba.applet.JavaBridge.openOutputStream(JavaBridge.java:533)
            at waba.applet.JavaBridge.writeBytes(JavaBridge.java:588)
            at waba.io.NativeMethods4JDK.catalogClose(NativeMethods4JDK.java:824)
            at waba.io.NativeMethods4JDK$CatalogBag.finalize(NativeMethods4JDK.java: 674)
            at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
            at java.lang.ref.Finalizer.runFinalizer(Unknown Source)
            at java.lang.ref.Finalizer.access$100(Unknown Source)
            at java.lang.ref.Finalizer$3.run(Unknown Source)
            at java.lang.Thread.run(Unknown Source)
    #2- url: file://localhost/nullfile.pdb
    found under localhost: file://localhost/nullfile.pdb
    file not found

  • Burning process never ends

    When I try to burn a CD, everything works fine until it begins "finishing" the disc. This process never actually finishes. I've let it run for more than an hour. I've ruined a handful of CDs already

    Had that a while ago, too with an old version of iTunes. I do not know why this happened. Although iTunes told me that it is still finishing the disc, I stopped that process and got the disc out of it's drive and I was surprised that the disc played fine.
    If you use an old version, try to update your iTunes.
      Windows XP   iPod mini 2nd gen.

  • Should NTP Processes Never End?

    For my NTP needs, I've installed openntpd, removed /etc/cron.hourly/adjtime, and added this line to my crontab,
    * * * * * ntpd -s && hwclock -w > /dev/null 2>&1 && rm /var/lib/hwclock/adjtime
    It works like a charm; however, I have noticed that the ntpd processes never seem to complete. Here's a snippet from my "ps aux" output,
    root 11746 0.0 0.0 18196 468 ? Ss 02:10 0:00 ntpd -s
    ntp 11764 0.0 0.0 13984 860 ? S 02:11 0:00 ntpd -s
    root 11769 0.0 0.0 18196 344 ? Ss 02:11 0:00 ntpd -s
    ntp 11780 0.0 0.0 13984 860 ? S 02:12 0:00 ntpd -s
    root 11785 0.0 0.0 18196 340 ? Ss 02:12 0:00 ntpd -s
    ntp 11796 0.0 0.0 13984 860 ? S 02:13 0:00 ntpd -s
    root 11801 0.0 0.0 18196 344 ? Ss 02:13 0:00 ntpd -s
    ntp 11916 0.0 0.0 13984 860 ? S 02:14 0:00 ntpd -s
    And if I pipe the ps aux output through grep and wc, I see that there are over a thousand unended ntp-related processes after running the machine for 9 hours,
    [root@reformedtube ~]# uptime ; ps aux | grep ntp | wc -l
    02:25:19 up 9:01, 1 user, load average: 0.06, 0.02, 0.00
    1084
    Does anyone have any ideas?

    Yes you are completely right, I normally run it in a terminal so I forgot about that (I should have read the man page better ).
    You would need to do something like:
    ntpd -d -s & sleep 5 && pkill -n ntpd
    EDIT: or even:
    ntpd -d -s & sleep 5 && kill $!
    Last edited by quigybo (2010-06-21 11:39:01)

  • Is the iTunes Match Updating (Library) Process Never Ending For You Too?

    I think that I added about 1200 songs to my iTunes Library over the past 3 days. A lot of those songs are obscure and are certainly NOT in the iTunes store so it will have to upload them, but this latest updating process is going so slow it seems like it might be stuck. I rebooted my system and started iTunes again. Maybe this will help. What's odd is that the process seems to linger near the end of 'Step 2: Matching Your Songs With Songs in The iTunes Store' indefinitely--which seems odd. I would think that if it was an 'uploading issue' that 'Step 3: Uploading artwork and remaining songs' would be where the process would hang up and not on Step 2, which should seemingly fly by.
    Has anyone else experienced this hang nail with iTunes Match?
    We have good speed broadband (cable supplied) Internet service through Time Warner.

    ST67 wrote:
    Going forward, I think Apple needs an iCoud/iTunes Match 'System Status Page' that users can check before they undergo big uploads at home.. Dropbox and many other Cloud-Based Services do that and I think Apple could certainly benefit from cluing in their customer base in Real Time.
    http://www.apple.com/support/icloud/systemstatus/
    But notice the date and status.  I also heard from phone support that this is really for iCloud and not necessarily iTunes Match itself.   I agree that they should extend this out to iTM as well - would make a lot of sense and would avoid the confusion we are experiencing.

  • How to control CMD.exe STDIN and STDOUT through CVI

    Hi,
    I am woking on one applicaiton which is used for debugging the arm processor with Jlink ICD. I am having the command promt Jlink.exe which peforms all the required operations. All the commands(STDIN and STDOUT) want to redirect through my CVI LabWindows and the Jlink.exe should be hidden.

    Hello Mate.
    I need your help... like you, I have a Pavilion Laptop with an ATI 6770m video card.
    But I did screw it the GPU Bios, so i can't use it anymore, i'm just working with the intel One.
    So I want to ask you if you can send me a copy of your GPU Bios.
    If yes, there are several ways, but the secure one is using GPU-Z located in this link, http://www.techpowerup.com/downloads/2360/techpowerup-gpu-z-v0-7-8/
    using this tool, easily you can choose the proper GPU at the bottom of the tool window, and just in front of the bios information it collects, is the download button.
    So if you please download it and share it with me, I’ll really appreciate that.
    My mail is like my user at aim.com
    Thanks in advance.
    Best regards. 

  • Requirement is to run CMD.EXE under the Local System Account. So that we can map a network drive to be used by a windows service, which will be created by command: - net use z: \\servername\sharedfolder /persistent:yes

    Environment:
    OS:  Windows 7 32/64 bit, Windows 2008 Server 64
    bit/ Windows 2012 Server 64 bit
    Priority:
    - Critical
    Requirement: - Since
    the Windows Service is running under the Local System Account, we would like to emulate this same behaviour.
    Basically, we would like to run CMD.EXE under the Local System Account. So that we can map a network drive to be used by a service using following
    command
    net use z: \\servername\sharedfolder /persistent:yes.
    Already Attempt:
    We tried to launch the CMD.exe using the DOS Task Scheduler AT command.  Here’s a sample command:
    AT 10:36 /interactive cmd.exe
    But I received a warning that “due
    to security enhancements, this task will run at the time excepted but not interactively.”
    It turns out that this approach will work for XP, 2000 and Server 2003 but due to session isolation
    Interactive services no longer work on Windows 7, Windows Server 2008 and above.
      2.  We
    tried to create a secondary Windows Service via the Service Control (sc.exe) which merely launches CMD.exe.
    <Drive>:\sc create RunCMDAsLSA binpath= "cmd" type=own type=interact <Drive>:\sc
    start RunCMDAsLSA
    In this case the service fails to start and results it the following error message:
    FAILED 1053: The service did not respond to the start or control request in a timely fashion.
      3. One
    suggestion, we found to launch CMD.exe via a Scheduled Task, but
    it is not giving any option to launch CMD.exe in interactive mode; so that I can map network drive using net command.
      4. I read an article, which
    demonstrates the use of PSTools from SysInternals. I launched the command line and executed following command
    psexec -i -s cmd.exe
    PSTools worked fine, but It seems that in scope of Sysinternals Software License
    Terms. You may not "use the software for commercial software hosting services."
    Application will deploy on client, which will be like commercial,
    so we are not able to use PSTools.         
    Kindly assist us for achieving the requirement. We have tried all the ways, but nothing is working for us. Kindly suggest.
    I will be really thankful.

    Hi Sir,
    Nothing worked from above for us. You can see our remarks on posted query.
    That’s why, we posted on forum.
    And there will not be any vulnerability, because, if we will use "net
    use ..."
    in network domain; definitely,
    we will provide username and password of mapped drive system.
    And, that system, itself is given by client; so that, there must not be any vulnerability; they are ready to provide user name and password.
    We need a way; by which we can complete the requirement. Kindly assist.
    Regards,
    S. P. Singh

  • PFI Compilation never ends

    Hi,
    I have been developping my app since 2 month now and I never had any problem with creating test ipas (ie:target = ipa-test). But when I decided to create my final build for the app store (target = ipa-app-store) the process never ends.
    I run the compilation and it seems to be doing something (I can see the temp files and hear the fans) but it just doesn't stop. I tried many times and usually kill the process after an hour or so.
    I know that other people had a similar problem but I didn't find any official answer for it. Is it a bug? Does it have a workaround?
    I'm on a Mac with Snow Leopard and tried to build the ipa from both Flash CS5 and a custom ant build.
    I would really appreciate some feedback about it.
    Cheers,
    Ben

    First and foremost, compiling for iOS takes a lot of resources, it cannot be compared to packaging for Android, because the process is completely different, AIR on Android runs interpreted code, while on iOS it is native code, before deciding the compilation is stuck you should check the java/adt processes aren't doing anything. It's been said future AIR versions will reduce iOS compilation times.
    Second, I've seen iOS compilation fail and take forever only when using certain ANEs and Flash Builder versions. FB doesn't take too well certain ANE warnings and errors, and instead of ignoring or finishing the process showing some alert, it just sits there making you wait forever. If you are using ANEs in your project, you should isolate the problematic one, and then determine what may be the cause... if some problem with the iOS SDK used with your AIR SDK, if the ANE is causing some unimportant warnings but your IDE doesn't like them, etc.

  • How to end a process from cmd.exe?

    I need to open and close a word file(Test.doc) from my java program. I managed to open the file using cmd shell, but I don't know how to close the opened file. Does anybody have a suggestion? Below is my code. Thanks.
        Runtime r = Runtime.getRuntime();
        Process p = null;
        try {
        p = r.exec("cmd.exe /c start Test.doc");
        catch (Exception e) {
          System.out.println("Error opening the file.");
        }

    Try something like this:
    Runtime r = Runtime.getRuntime();
    Process p = null;
    String command = "\"C:\\Program Files\\Microsoft Office\\Office12\\winword\"";
    String file = "\"c:\\Test.doc\"";
    try {
        p = r.exec(command + " " + file);    //to open the file in MS Office Word
        Thread.sleep(30000);    //keep it open for 30secs
        p.destroy();    //close the process and hence the file
    }catch (Exception e) {
        System.out.println("Error opening the file.");
    }Edited by: Sourav-Sipani on Jul 28, 2008 1:56 AM

  • Verity vspider.exe process never stops and sucks system resources

    I'm running a scheduled task to index a site on a Win2k3 server running CF8. I'm having a problem where the vspider.exe process never stops and sucks up system resources, slowing my whole server down. It actually is doing quite a bit of work... I'm directing the output into a log file, and I'm getting 300 K of information and it seems to be walking the site just fine. But it ends up not finishing the job and running forever. I have to open the task manager to terminate the process and then the server runs better.
    Any ideas?

    I have not found an answer to this problem, though it hasn't been a problem lately. I now send the output of the vspider.exe process to a log file and can check the status of the index job. I index nightly, so if the process is running at anytime, I can look at the results of the index job in my log file. This might give some clues. I have a feeling I had a corrupt index and vspider was choking. Try rebuilding your index from scratch.

  • Execute Script (batch processing) by cmd.exe?

    Hi! Is there a possibility to execute some scripts (batch processing) via the command line (cmd.exe)?
    It's very important to me to know if there still exist a possibility (and of course I like to know how it works and what I have to type...)
    Thanks in advance!

    No, there is not.
    Leonard

  • When i open reader two AcroRd32.exe*32 processes open and never close even after exiting the reader.

    when i open reader two AcroRd32.exe*32 processes open and do not close after closing the reader. i have seen 10 AcroRd31.exe*32 processes open. do I have an undetectable virus or is the software just poorly written and tested. I am running Windows 7 premium that is fully up to date and the reader is up to date also. i have even deleted reader and reinstalled it.
    All 8 Processors activity jumps really high while AcroRd.exe*32 is running with no pdf documents open and as soon as i end the processes the processor activity goes down to near 0 according to the performance tab in task manager. My fan also kicks on because of all the heat from the processor.

    open acrord32.exe and go to edit, preferences, internet, and then uncheck "Allow fast web view" and "allow speculative downloading in the background". This will save your processor and keep your system from locking up from all of the acrord32.exe processes running.

  • How to create a activex control to execute cmd.exe with parameters?

    Hi,
    Is there any way to execute a cmd.exe on client side using activex.
    currently i am using vs-2013
    Please help i have no idea about activex control.
    Pawan

    No, there is certainly no server side setting that will allow you to do this. Please refer to the following links for more information:
    http://stackoverflow.com/questions/1791511/run-exe-from-client-side
    http://stackoverflow.com/questions/5787200/how-to-open-command-prompt-from-my-application-client-side
    http://p2p.wrox.com/javascript-how/24362-how-call-shell-cmd-open-exe-clientside.html
    Please remember to mark helpful posts as answer and/or helpful.

  • Process prcs2 = rt.exec("cmd.exe /c \"e:\\filename\"");  How in UNIX?

    I have this current process running from a servlet on a NT server. It is kicking off an excel macro which creates a graph in excel, and then displaying the graph. I want to kick off a command in UNIX from a different servlet. What would I have to use? If anyone knows, please email me at [email protected]
    Justin Mennen

    I would like to run a shell script on OS390 from my servlet. How would I do it? I have a similar process that works with NT listed below. What would I do to run a script on UNIX immediately from the servlet?
    Process prcs2 = rt.exec("cmd.exe /c \"e:\mydirectory\filename\"");

Maybe you are looking for

  • Error while creating Time Card Using API

    Hello I have a requirement to run OTL Interface once day to create/update Time Card in OTL for those datea created on sysdate-1 in Service Module (data will get feeded from Service Module). For example this interface will run on sysdate to create tim

  • DV6 3019 TU FAN

    I have a HP Pavillion Dv6-3019TU laptop. OS is Windows 7. There was a problem with heating and fan noise from the purchase dare. I went to the HP Service Centre, but the problem didnt solve. Before a week ago the fan gone crazy, it started making noi

  • IOS5 TV Out Settings?

    I'm trying to connect my iPad2 with iOS5 to my TV using the Apple Composite AV Cables.  The audio works fine, but the video is visible, but scrolling like the vsync is off. I've tried the same TV and same cables with my daughter's iPod Touch Gen 4 wi

  • Invetsprogram in the assgnments of the PM Order.

    Dear experts, i should maintain during the run time a value to the invetsprogram in the assgnments of the PM Order. The value came from a customer Z-Table Now i search a Badi or an Customer Exit which this is possible. (or another solution.) Greeting

  • Error when I try to render

    Hello, I just downloaded a template on the internet and I want to render it now, But when i try to render it I get the error: 2 footage files required to render composition "Template by IntroArtZ" are missing. How can I fix this?