0
package src;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; 

public class Command
{ 
    public static void main(String args[]) 
    { 
        try 
        {
            Process p=Runtime.getRuntime().exec("cmd /c dir"); 
            p.waitFor(); 
            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
            String line=reader.readLine(); 
            while(line!=null) 
            { 
                System.out.println(line); 
                line=reader.readLine(); 
            } 

        } 
        catch(IOException e1) {} 
        catch(InterruptedException e2) {} 

        System.out.println("Done"); 
    } 
} 

I tried to run this code but it is not able to execute anything. Are there some classpath or other settings i need to do to make this work?

5
  • 2
    What do you mean by 'not able to execute'? What is the console output when you run this? It's also possible that it's running but that an exception is thrown. You are not doing anything with exceptions. Add a e1.printStrackTrace(); in the exception blocks to see whats wrong Commented Jul 2, 2012 at 8:31
  • 1
    Compile it >javac Command.java -d . and Run it from command prompt >java src.Command Commented Jul 2, 2012 at 8:31
  • means it's doing nothing! It is just trapped in an infinite loop and not giving any output. Commented Jul 2, 2012 at 8:35
  • 1
    Well, 'doing nothing' is very different from 'looping indefinitely'. If it is not giving output it's probably not in the loop (because the loop prints). It's probably stuck at the first readLine(), waiting for input or waiting at the waitFor(). Related question was answered here: stackoverflow.com/questions/8149828/…, it has the waitFor() after the loop Commented Jul 2, 2012 at 8:40
  • The code works fine for me? output being: Volume in drive C has no label. Volume Serial Number is CC8C-8FCE Directory of C:\Users\David\Documents\NetBeansProjects\Command 2012/07/02 10:43 AM <DIR> . 2012/07/02 10:43 AM <DIR> .. 2012/07/02 10:43 AM <DIR> build 2012/07/02 10:43 AM 3�716 build.xml 2012/07/02 10:43 AM 85 manifest.mf 2012/07/02 10:43 AM <DIR> nbproject 2012/07/02 10:43 AM <DIR> src 2 File(s) 3�801 bytes 5 Dir(s) 92�880�769�024 bytes free Done Commented Jul 2, 2012 at 8:45

1 Answer 1

2

remark the line:

p.waitFor(); 

and re-run.

You also might want to read this

Sign up to request clarification or add additional context in comments.

11 Comments

ok and alfasin what exactly would i have to write if i want to run a java program suppose a.java? What do i have to change?
javac a.java and then java a (from prompt)
yeah but i want that this all should be done by the program which i have posted. So is there a way that this program automatically compiles and runs the program?
what do you mean by "automatically" ? scheduled task (cron) ?
no, i mean that i will run this java program which will automatically compile another java program and run it. My main aim is that i don't want to go to terminal to compile and run a java program. I want to run commands via this java program.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.