0

I want to compile and run the java file at the server end and output to be viewed at the client end,so when asked for input,can be supplied by programmer. So far I have used the Java Compiler API to compile the java code.

The code is as follows :

public class CompileCode extends Thread {
File javafile,errorfile;
String parameters;
String compileoutput; 
File err;


public CompileCode(File f,String parameters)
{
    javafile = f;
    this.parameters=parameters;
    err=new File("Errors.txt");

}

@Override
public void run()
{       
    try{   
    FileOutputStream errorstream=new FileOutputStream(err);
    FileInputStream inputstr=new FileInputStream(out);
    JavaCompiler compilerinstance=ToolProvider.getSystemJavaCompiler();
    int compileresult=compilerinstance.run(null, null, errorstream,javafile.getAbsolutePath());
    errorstream.close();
    out.close();
    if(compileresult==0)
    {
        compileoutput="Compilation Successful";
    }
    else
    { 
        compileoutput="Compilation failed";   
    }
}
    catch(IOException ex1)
    {
        System.out.println(ex1.getMessage());
    }
}
    public String getOutMessage()
    {
        return compileoutput;
    }    

    public String getErrorlog()
    {
        String output=" ",line=" ";
        StringBuffer sb=new StringBuffer();
        try{
        BufferedReader br =new BufferedReader(new FileReader(err));
        while((line=br.readLine())!=null)
        {
            sb.append(line+"\n");
        }
        output=sb.toString();
        }
        catch(Exception ex2)
        {
            System.out.println(ex2.getMessage()); 
        }
        finally
        {
            return output;
        }
    }

}
The code for reading the output is:

    ProcessBuilder pb =new ProcessBuilder("java ",filename);
    pb.directory(fl.getParentFile());
    pb.redirectError();
    Process p = pb.start();
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    String line;
    StringBuffer sb=new StringBuffer();
    while((line=br.readLine())!=null)
    {
        sb.append(line+"\n");
    }

    line=sb.toString();
    Outmessage=Outmessage.concat(line);
    p.waitFor();

where "fl" is the .java file,and filename is the name of file without extension.

The Exception occured is :

 java.io.IOException: Cannot run program "java -cp . " (in directory "C:\Users\Mayank"): CreateProcess error=2, The system cannot find the file specified
Mayank
  • 19
  • 6
  • Maybe the error message "The system cannot find the file specified" gives a hint? (You should add the full path to the executable.) – Seelenvirtuose Mar 28 '15 at 07:13
  • I did add the full path to file name for ex. for Example.java which is stored in C:\Users\Mayank I added filename="C:\Users\Mayank\Example" but still it shows the same error – Mayank Mar 28 '15 at 07:29
  • Do you really have (or think you have) a "java.exe" in the directory "C:\Users\Mayank"? Hint: I was speaking about the _executable_. You are calling an executable. This is like asking your OS: "Hey, please start 'java' for me." – Seelenvirtuose Mar 28 '15 at 07:32
  • Take a look at this link: http://stackoverflow.com/questions/4842684/how-to-compile-run-java-program-in-another-java-program – alainlompo Mar 28 '15 at 07:41

0 Answers0