Question

I run in command line the following program as an example app:

java -cp "D:\projects\PDFJavaFX\lib\PDFRenderer-0.9.1.jar" com/sun/pdfview/PDFViewer

Then I run in command line the JDI trace example:

java -cp "C:\Program Files\Java\jdk1.7.0_13\lib\tools.jar;D:\projects\JDI_Trace\jar\trace.jar;D:\projects\PDFJavaFX\lib\PDFRenderer-0.9.1.jar" com/sun/tools/example/trace/Trace com/sun/pdfview/PDFViewer

I get this error:

Error: Could not find or load main class com.sun.pdfview.PDFViewer
 -- VM Started --
 -- The application exited --

The example app runs correctly, and it is included in the classpath. What's the cause of this? What am I missing?

Thanks

Was it helpful?

Solution

Edit: It looks like it is classpath related.

I did get this to work (well, it popped up the GUI but then crashed pretty quickly). I used the classpath environment variable instead of -cp:

C:\cos126\dev\debug>set CLASSPATH=%CLASSPATH%;c:\tmp\PDFRenderer-0.9.1.jar;c:\tmp\debug

So, not pretty, but then it did work. So it looks like the newly created VM doesn't automatically inherit -cp. I am optimistic, but not sure, that there might be an option you can change when starting the new VM to do this for you. To see the "documentation" for the VM launching options, you can add some code like

for (Map.Entry<String, Connector.Argument> arg : arguments.entrySet()) {
   System.out.println(arg.getValue().name()+" "+arg.getValue().description());
}

to Trace.java. When I do this, it prints out

home Home directory of the SDK or runtime environment used to launch the application
options Launched VM options
main Main class and arguments, or if -jar is an option, the main jar file and arguments
suspend All threads will be suspended before execution of main
quote Character used to combine space-delimited text into a single command line argument
vmexec Name of the Java VM launcher

so maybe one of those is useful? Good luck!

By the way, this is what I used JDI for:

http://cscircles.cemc.uwaterloo.ca/java-visualize/

I am in the process of making the source shareable, if you want to see it (although I'm not 100% sure it will be of use).

OTHER TIPS

Your command : java -cp "C:\Program Files\Java\jdk1.7.0_13\lib\tools.jar; D:\projects\JDI_Trace\jar\trace.jar; D:\projects\PDFJavaFX\lib\PDFRenderer-0.9.1.jar" com/sun/tools/example/trace/Trace com/sun/pdfview/PDFViewer

Explanation : The new VM which is created by the Trace has different class path. main class PDFViewer is in the PDFRenderer**.jar,but the new VM didn't know the jar, so it can't find the main class. I also met this problem when I used Eclipse. And by changing the working directory, I can run it successfully.

In fact, the Trace class uses JDI to launch the new VM, but it only set the main option and discard the vm options. The code below is quoted from the Thrace class source file, and I add some lines to print the options.

Map<String, Connector.Argument> connectorArguments(LaunchingConnector connector, String mainArgs) {
    Map<String, Connector.Argument> arguments = connector.defaultArguments();
    Connector.Argument mainArg =
                 (Connector.Argument)arguments.get("main");
    //added by me: begin
    java.util.Set<String> argsString = arguments.keySet();
    System.out.println("connector args size is :" + argsString.size());
    for (String arg : argsString) {
        System.out.println(arg + "=="+ arguments.get(arg).description()+"=="+arguments.get(arg).value()) ;
    //added by me: end
    }
    if (mainArg == null) {
        throw new Error("Bad launching connector");
    }
    mainArg.setValue(mainArgs);

The output of the arguments size is 6, and they are "home, options, main, suspend, quote and vmexec". If we want to configure the new VM options, we can set the "options" by setValue method like setting "main".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top