-1

I try to using Runtime but have some problem. I want to use Runtime.exec("java -cp"). One is jar file, another is class file. Class file is refer to jar file but not included. Below is my code

  Runtime rt = Runtime.getRuntime();
    Process proc = null;

    System.out.println(Run.class.getResource("").getPath());
    try {
        proc = rt.exec("java -cp c:/data/hh.jar; com/list/shark/Whale");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Result is "could not find or load default class". I really want to know that where is location executing runtime and how sole this problem.

PS In local cmd, command is working.

ho9
  • 42
  • 6
  • Don't use quote formatting for text that isn't quoted. Don't post pictures of text: post the text. Unclear what you're asking. – user207421 Dec 23 '16 at 08:34

2 Answers2

0

The command you are using is wrong.

For running a jar from command line:

java -jar <jar-file-name>.jar

If you did not create a manifest file in your jar, java -jar will not work. Then you will need to specify the FQP (fully-qualified classpath) explicitely in the command:

java -cp <fully.qualified.class.path>

There is a good answer on how to create and run jar that you might want to check out

Community
  • 1
  • 1
aviad
  • 8,229
  • 9
  • 50
  • 98
0

I post answer for someone who have same trouble

just do

 RunTime.exec("cmd /c dir");

It is for find out executing current path

and change from exec(command) to exec(command,env,file).

create fileand input executing current path

 File file = new File("c:/Test/");

 RunTime.exec("java -cp c:/data/hh.jar; com.list.shark.Whale",null,file);

this is success for execute class File referencing other jar file.

ho9
  • 42
  • 6
  • What does `cmd /c dir` have to do with it? All it does is tell you the value of `System.getProperty("user.dir")`, and you don't need to exec anything for that. – user207421 Jan 02 '17 at 05:23