0
import java.io.*;

public class chk 
{
String className;
String command,command1,command2;
public String  getMsg(String fileName,String Path) 
{
    String dir;
    command1="cd "+Path;
    dir=Path.charAt(0)+Path.charAt(1)+"";
    command2=dir;
command = "javac " + fileName;
    String a=executeCommand(command1);
    a=executeCommand(command2);
String output = executeCommand(command);
if(output.compareTo("")==0)             
        output = "Compilation Successfull!!";
    return output;
}
private String executeCommand(String command) 
{
    StringBuffer output = new StringBuffer();
    Process p;
    try 
    {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";           
        while ((line = reader1.readLine())!= null) 
        {
            output.append(line + "\n");
        }
        while ((line = reader2.readLine())!= null) 
        {
            output.append(line + "\n");
        }
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
    return output.toString();
}
public static void main(String args[])throws IOException
{
        String x;
    chk ob=new chk();
    x=ob.getMsg("MyClass.java","D:\test");
    System.out.println("OUtput : "+x);
}
}

Error

enter image description here

I am trying to run a bunch of commands in command prompt with the help of a java file so that later i can compile another java file named "MyClass.java" which is present at some other drive in my computer but i am getting the following error stating it cannot even execute my first command i.e "command1="cd "+Path;" this line . pls help!

rick
  • 913
  • 6
  • 13
  • 28

1 Answers1

3

cd is not a program on Windows. If you open a Command Prompt window, this runs a program cmd.exe that inputs and handles the commands. Many commands will cause programs to be executed, but some commands are interpreted by cmd.exe itself, including the cd command. And the cd command will set up some state in the Command Prompt window that will affect how the same cmd.exe handles future commands. Because of this, not only can you not run cd as a program, you also cannot run cmd.exe and use it to process a cd command. You could, but it won't do you any good, because the cd command will only affect what goes on inside that cmd.exe process, and then cmd.exe will terminate.

You probably want to look into ProcessBuilder, which has a method directory to set the working directory of the process. (I'm not real familiar with this class, so I can't give you any specific examples. But it does look like it's what you need.)

EDIT: After looking into this further: You're using the exec method of Runtime. The exec method has a version that takes the working directory as a parameter:

public Process exec(String command,
                    String[] envp,
                    File dir)

So if you use this with null for envp (assuming you don't want to create a new set of environment variables), and with a File set up to refer to the working directory, I think this will give you what you want. So you may be able to do things this way instead of using ProcessBuilder.

MORE: For a command like

java zzzzzz < C:\iptest\input.txt > C:\outtest\name.txt

When you type this command in a Command Prompt window, the cmd.exe program interprets the < and > commands to redirect input and output, and takes care of the needed actions. They won't work in a command executed by exec() because they'll just be treated as command-line arguments. The exec method of Runtime doesn't have a mechanism for setting up redirected input and output files, but ProcessBuilder does. See the javadoc. I don't have much experience with ProcessBuilder, but it looks like you need to create a ProcessBuilder object, use command to set up the command and arguments (as separate strings, not as one long string with space characters), use directory to set the working directory, use redirectInput and redirectOutput to set up the files for redirection, and then start().

ajb
  • 31,309
  • 3
  • 58
  • 84
  • what i will give in place of 3rd parameter ? – rick Feb 07 '14 at 19:54
  • i have a path i.e "D:\test" how to cast it into a file ? – rick Feb 07 '14 at 19:55
  • 1
    `new File("D:\\test")`. This is in the documentation for the `File` class. – ajb Feb 07 '14 at 20:03
  • how to compile "command ="java " + name +" < C:\\iptest\\input.txt > C:\\outtest\\"+name+".txt";" in the same way – rick Feb 14 '14 at 16:59
  • see my question [link](http://stackoverflow.com/questions/21785626/executing-a-class-from-a-java-file-with-java-commands-in-it) – rick Feb 14 '14 at 18:08
  • sorry to bother you again the previous java command problem is solved but now when i am compiling with "sum.exe < C:\iptest\input.txt > C:\outtest\sum.txt" this command. The output file gets generated with nothing stored in it i.e a 0KB file is formed . Pls HELP! [link](http://stackoverflow.com/questions/21802736/executing-c-command-from-a-java-file?noredirect=1#comment32991835_21802736) – rick Feb 15 '14 at 19:52