Could not find or load default class

71 views Asked by At

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.

2

There are 2 answers

4
aviad On

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

1
ho9 On

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.