1

The following code(when executed) prompts the user to enter any java class name to execute.

import java.io.*;
public class exec {

public static void main(String argv[]) {
    try {
        InputStreamReader isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(isr);

        System.out.println("Enter the java class name");
        String s=br.readLine();

        Process pro=Runtime.getRuntime().exec(s);

        BufferedReader in=new BufferedReader(new InputStreamReader(pro.getInputStream()));
        String line=null;
        while((line=in.readLine())!=null) {
            System.out.println(line);
        }
        in.close();
    } catch(Exception err) {
        err.printStackTrace();
    }
}

This code works fine if I'm using command prompt and I'm able to execute another java program. But I'm unable to do the same using eclipse.No output or error is showing up once I enter the java class name.

I'm new to eclipse. Need help.

2 Answers 2

3

You can't "execute" a java class, so your code as posted can't work.

Instead, you'll need to execute "java" and pass to it the classpath and class name as parameters, something like this:

String s = br.readLine();

String[] cmd = {"java", "-cp", "/some/path/to/your/jar/file", s};

Process pro = Runtime.getRuntime().exec(cmd);
Sign up to request clarification or add additional context in comments.

10 Comments

which path do i need to add in "/some/path/to/your/jar/file".And should i add the class path of the java class i'm calling in "-cp".
I thought it was pretty clear that "/some/path/to/your/jar/file" was the path of the jar file that contains the class you want to run, ie the classpath. If you have dependencies (other jars your jar needs to run) put all the jars in a directory and make the classpath that directory. I tested this code and it works for me, assuming "s" is "com.mycompany.MyClass" for example
I'm sorry to ask you again. I'm really confused with the path.I'm trying in this way String[] cmd={"java","C:/Users/BHARAT/workspace/Testing",s}; In this line C:/... is the path of the project in which all the classes are located. Is this correct?
No. Just copy my code exactly but with the path to your jars! Let's put it this way: Find the command that works in windows, for example let's say this command works: java -cp C:/Users/BHARAT/workspace/Testing com.bharat.util.TestClass, then your code would be String[] cmd = {"java", "-cp", "C:/Users/BHARAT/workspace/Testing", s};, where s = "com.bharat.util.TestClass";. Every "word" of the command is an element of the cmd array.
Thanks a lot. Its working. I was copying the wrong path every time.
|
0

Do you just enter the name of the class, or do you also enter the directory of where the program is located? I think it doesn't work in eclipse because you need to specify where the file is... like C:\Users\Me\workspace\ProjectName\src\filename

2 Comments

if i use command prompt i'll enter 'java classname'. but this is not working in eclipse
In eclipse, try typing in the location of the file and see if it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.