We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,894 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help in passing arguments

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...

import java.io.*;
import java.io.InputStream;
import java.io.OutputStream;

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\\tests.java";
            child = Runtime.getRuntime().exec(command);
            InputStream in = child.getInputStream();
            OutputStream out = child.getOutputStream();
            try {
                String message = "95"; //Marks is given as 95
                out.write(message.getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }
            int c;  //to read from the output as your mark is 95 after passing on to the test.java
            while ((c = in.read()) != -1) {
                System.out.print((char) c);
            }
            in.close();
        } catch (Exception e) {
            int k = 0;
        }
    }
}

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

import java.io.*;
public class tests {
    public static void main(String args[])throws IOException
    {
        String mark;
        int marks;
        BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter your mark :");
        mark=bfr.readLine();
        marks=Integer.parseInt(mark);
        System.out.println("Your mark is:" +marks);
    }

}

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

2
Contributors
1
Reply
1 Day
Discussion Span
2 Years Ago
Last Updated
2
Views
ajijacobm
Light Poster
39 posts since Aug 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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.

~s.o.s~
Failure as a human
Administrator
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0594 seconds using 2.69MB