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?

agarwav
  • 400
  • 2
  • 8
  • 16
  • 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 – Joost Jul 02 '12 at 08:31
  • 1
    Compile it `>javac Command.java -d .` and Run it from command prompt `>java src.Command` – KV Prajapati Jul 02 '12 at 08:31
  • means it's doing nothing! It is just trapped in an infinite loop and not giving any output. – agarwav Jul 02 '12 at 08: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: http://stackoverflow.com/questions/8149828/read-the-output-from-java-exec, it has the `waitFor()` after the loop – Joost Jul 02 '12 at 08: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 . 2012/07/02 10:43 AM .. 2012/07/02 10:43 AM 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 nbproject 2012/07/02 10:43 AM src 2 File(s) 3�801 bytes 5 Dir(s) 92�880�769�024 bytes free Done – David Kroukamp Jul 02 '12 at 08:45

1 Answers1

2

remark the line:

p.waitFor(); 

and re-run.

You also might want to read this

Community
  • 1
  • 1
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 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? – agarwav Jul 02 '12 at 08:49
  • `javac a.java` and then `java a` (from prompt) – Nir Alfasi Jul 02 '12 at 08:50
  • 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? – agarwav Jul 02 '12 at 08:52
  • what do you mean by "automatically" ? scheduled task (cron) ? – Nir Alfasi Jul 02 '12 at 08:53
  • 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. – agarwav Jul 02 '12 at 08:55
  • depends on the OS that you're working on. If you're on Windows, you can create a batch file with the commands I wrote on the first comment. If you're on Linux, you'll have to write a small shell script that executes these two commands etc. – Nir Alfasi Jul 02 '12 at 08:58
  • The program is able to run "cmd /c dir" but commands like "javac" aare not working. I don't know what actually is the problem. (And I am doing all this in Windows) – agarwav Jul 02 '12 at 09:00
  • you need either to install Java or to set the `PATH` variable to include the path to your %Java% and %Java/bin% directories. – Nir Alfasi Jul 02 '12 at 09:01
  • In Win7 you can do it by right-click on "My Computer", choose: "properties", then "Advance System properties" and create/update the environment variable "PATH" – Nir Alfasi Jul 02 '12 at 09:04
  • I did this and now its able to show the output of "java" but not "javac" or "java -version" – agarwav Jul 02 '12 at 09:10
  • so you need to find the location of "javac" and make sure it's set in the PATH. Test it from command prompt to make sure it works. – Nir Alfasi Jul 02 '12 at 16:16