9 Replies Latest reply: Apr 29, 2008 6:01 PM by 843810 RSS

    How can I run VM with the specified Path.

    843810
      I know my problem, but I did not know how to solve it. I am trying to run trace program and I used:
      JFileChooser fc = new javax.swing.JFileChooser();
      The problem is when I try to run VM using the class name I got this error:

      java.lang.
      Caused by: java.lang.ClassNotFoundException: HelloWorld
      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
      Exception in thread "main"
      -- VM Started --

      the code is as the following :

                          String className= FileName.substring(0, FileName.length()-5);
                          System.out.println( "Class Name = "+ className);      
                               vm = launchTarget(className);//ClassPath
                               EventRequestManager mgr =      vm.eventRequestManager();
                          writer = new PrintWriter(System.out);
                          generateTrace(writer);
      How can I run VM with the specified Path.
      Note that the class name like
      Class Name = HelloWorld

      Note that if the class is in the same directory the program works fine. i.e if trace.java and HelloWorld.java on the same directory
      Is there any methods to help me to set the spcified path? Really thanks for any help.

      Edited by: Iman_S on Apr 16, 2008 5:00 PM
        • 1. Re: How can I run VM with the specified Path.
          843810
          My problem is equivalent to if I want to run java VM from DOS prompt when both are in different directory. I mean if I compile via dos like the following:

          C:\Program Files\Microsoft Visual Studio 8\VC>java C:\java\HelloWorld

          I got the following error:

          Exception in thread "main" java.lang.NoClassDefFoundError: C:\java\HelloWorld
          Caused by: java.lang.ClassNotFoundException: C:\java\HelloWorld
          at java.net.URLClassLoader$1.run(Unknown Source)
          at java.security.AccessController.doPrivileged(Native Method)
          at java.net.URLClassLoader.findClass(Unknown Source)
          at java.lang.ClassLoader.loadClass(Unknown Source)
          at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
          at java.lang.ClassLoader.loadClass(Unknown Source)
          at java.lang.ClassLoader.loadClassInternal(Unknown Source)
          I tried to execute the command CD from my program then call vm = launchTarget(className);

          Process proc = null;
                              try {   
                              proc = Runtime.getRuntime().exec("CD "+ classPath); }
                              catch (IOException exp) {    exp.printStackTrace(); }
                              try {
                              proc.waitFor();
                                             
                              } catch (Exception exp2) {exp2.printStackTrace();    }

          I am really stuck on this any Help or comments please ?

          But I got an error like the following :
          ava.io.IOException: Cannot run program "CD": CreateProcess error=2, The system cannot find the file specified
          at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
          at java.lang.Runtime.exec(Runtime.java:593)
          at java.lang.Runtime.exec(Runtime.java:431)
          at java.lang.Runtime.exec(Runtime.java:328)
          at TemplateApp$1.actionPerformed(TemplateApp.java:236)
          at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
          at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
          at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
          at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
          at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
          at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1220)
          at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1261)
          at java.awt.Component.processMouseEvent(Component.java:6041)
          at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
          at java.awt.Component.processEvent(Component.java:5806)
          at java.awt.Container.processEvent(Container.java:2058)
          at java.awt.Component.dispatchEventImpl(Component.java:4413)
          at java.awt.Container.dispatchEventImpl(Container.java:2116)
          at java.awt.Component.dispatchEvent(Component.java:4243)
          at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
          at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
          at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
          at java.awt.Container.dispatchEventImpl(Container.java:2102)
          at java.awt.Window.dispatchEventImpl(Window.java:2440)
          at java.awt.Component.dispatchEvent(Component.java:4243)
          at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
          at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
          at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
          at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
          at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
          Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
          at java.lang.ProcessImpl.create(Native Method)
          at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
          at java.lang.ProcessImpl.start(ProcessImpl.java:30)
          at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
          ... 31 more
          java.lang.NullPointerException
          at TemplateApp$1.actionPerformed(TemplateApp.java:239)
          at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
          at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
          at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
          at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
          at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
          at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:1220)
          at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:1261)
          at java.awt.Component.processMouseEvent(Component.java:6041)
          at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
          at java.awt.Component.processEvent(Component.java:5806)
          at java.awt.Container.processEvent(Container.java:2058)
          at java.awt.Component.dispatchEventImpl(Component.java:4413)
          at java.awt.Container.dispatchEventImpl(Container.java:2116)
          at java.awt.Component.dispatchEvent(Component.java:4243)
          at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
          at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
          at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
          at java.awt.Container.dispatchEventImpl(Container.java:2102)
          at java.awt.Window.dispatchEventImpl(Window.java:2440)
          at java.awt.Component.dispatchEvent(Component.java:4243)
          at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
          at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
          at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
          at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
          at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
          • 2. Re: How can I run VM with the specified Path.
            800575
            Looks like your launchTarget method needs to add a -classpath option pointing to the dir or .jar that contains your HelloWorld.class.
            • 3. Re: How can I run VM with the specified Path.
              843810
              Hello,

              You said that you compile via dos like the following:
              C:\Program Files\Microsoft Visual Studio 8\VC>java C:\java\HelloWorld
              If you made that you are launching the execution not compiling the program code.

              If you are compiled it, my be you must pass an arguments in your execution command(after the main class name)
              • 4. Re: How can I run VM with the specified Path.
                843810
                Thanks a lot asmainf,

                Yes, you are right. I mean to execute not compile.
                What do you mean by my be you must pass an arguments in your execution command(after the main class name)
                I do not need to pass an arguments to the main method.

                The problem is how to make VM to see my current class path?
                • 5. Re: How can I run VM with the specified Path.
                  843810
                  Thanks a lot JimHolmlund, You are right. This is my problem, but how can I do that. I tried the following code, but my program Hangs. I do not what method to use to set the class path withen my program.
                  begin_

                  VirtualMachine launchTarget(String mainArgs) {
                  LaunchingConnector connector = findLaunchingConnector();

                  Map arguments = connectorArguments(connector, mainArgs);
                  *// Begin of the fix*
                  Connector.Argument optsArg =
                  (Connector.Argument)arguments.get("options");
                  if (optsArg == null) {
                  throw new Error("Bad launching connector");
                  *}// classpth is where the class located*
                  String cp = classpath +";" +System.getProperty("java.class.path");
                  optsArg.setValue(cp);
                  *//End of the fix*
                  try {
                  return connector.launch(arguments);
                  } catch (IOException exc) {
                  throw new Error("Unable to launch target VM: " + exc);
                  } catch (IllegalConnectorArgumentsException exc) {
                  throw new Error("Internal error: " + exc);
                  } catch (VMStartException exc) {
                  throw new Error("Target VM failed to initialize: " +
                  exc.getMessage());
                  }
                  }
                  end
                  • 6. Re: How can I run VM with the specified Path.
                    800575
                    Check out how jdb does this in jdb file TTY.java, found in <jdk>/demo/jpda/examples.jar
                    • 7. Re: How can I run VM with the specified Path.
                      843810
                      I have found the solution, but there is a small bug that I do not know how to resolve it. The problem is with the path itself. The code is as the follwing:

                      Connector.Argument options = (Connector.Argument)arguments.get("options");
                      options.setValue(classpath);

                      if the file located in a Class Path that has no spaces, the program works fine like : -classpath C:\java\testFolder
                      if the file located in a Class Path that has spaces, the program Hangs : -classpath C:\java\test Folder

                      Note that testFolder is the folder that contains the class file. Is there is any way to fix this?

                      Thanks
                      • 8. Re: How can I run VM with the specified Path.
                        800575
                        Try putting this in your classpath variable
                        -classpath "c:\java\test Folder"

                        Note the quotes around the path.
                        • 9. Re: How can I run VM with the specified Path.
                          843810
                          Yes, finally it works. Thanks a lot Jim. You are really great and supportive.

                          Here is what I did in case someone face the same problem:

                          classpath = "-classpath ";
                          classpath=classpath.concat("\"");
                          classpath=classpath.concat(classPath);// mind the difference btn classPath and classpath
                          classpath=classpath.concat("\"");

                          /************************************** Virtual Machine ***********************************************/
                          VirtualMachine launchTarget(String mainArgs) {
                          LaunchingConnector connector = findLaunchingConnector();

                          Map arguments = connectorArguments(connector, mainArgs);//mainArgs = Foo
                          Connector.Argument options = (Connector.Argument)arguments.get("options");
                          options.setValue(classpath);
                          try {
                          return connector.launch(arguments);
                          } catch (IOException exc) {
                          throw new Error("Unable to launch target VM: " + exc);
                          } catch (IllegalConnectorArgumentsException exc) {
                          throw new Error("Internal error: " + exc);
                          } catch (VMStartException exc) {
                          throw new Error("Target VM failed to initialize: " +
                          exc.getMessage());
                          }
                          }
                          //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////