Start New Discussion within our Software Development Community

hi guys,
Im doing a project that executes a java file through another java file.. I need to use Runtime.exec() funtion for the purpose. But, there is trouble in passing the input to the java file. Please help me solve the situation.

my main funtion is shown below...


public class Main {

    public static void main(String args[]) throws IOException {
        String command = "";
        Process child;
        BufferedWriter writer;
        try {
            command = "cmd /c start java E:\\file\\";
            child = Runtime.getRuntime().exec(command);
            InputStream in = child.getInputStream();
            OutputStream out = child.getOutputStream();
            try {
                String message = "95"; //Marks is given as 95
            } catch (Exception e) {
            int c;  //to read from the output as your mark is 95 after passing on to the
            while ((c = != -1) {
                System.out.print((char) c);
        } catch (Exception e) {
            int k = 0;

and also the java file which is to be executed from runtime.exec()

public class tests {
    public static void main(String args[])throws IOException
        String mark;
        int marks;
        BufferedReader bfr=new BufferedReader(new InputStreamReader(;
        System.out.println("Enter your mark :");
        System.out.println("Your mark is:" +marks);


The problem is the code is not working.. PLease help me.

You can't directly execute a Java file i.e. pass it to the 'java' process and execute it. You need to first compile it to a class file which is in turn passed to the 'java' command. Also, read this article which lists the pitfalls when using Runtime.exec.

This article has been dead for over six months. Start a new discussion instead.