Using exec() to run a class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Using exec() to run a class

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    Using exec() to run a class

    Hi,
    I would like to run a class file from my application. I managed to compile the java file, but got stack with executing it. To be more clear, i will post my code here:
    Code:
        public static void main(String args[]) throws Exception
        {
            try {
                String line;
                Runtime rt = Runtime.getRuntime();
                String[] cmd = { 
                    "c:/progra~1/Java/jdk1.5.0_05/bin/javac.exe", "c:/Temp/Test1/MainTest.java"};    
                Process p = rt.exec(cmd);
                int exitVal = p.waitFor();
                System.out.println("After compilation value: " + exitVal);
                
                String[] cmd2 = {
                    "cmd.exe", "/c","c:/progra~1/Java/jdk1.5.0_05/bin/java.exe -cp MainTest"};
                Runtime rt2 = Runtime.getRuntime();
                p = rt2.exec(cmd2);
                            
                BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                String temp = "";
                while((temp = input.readLine()) != null) {
                    System.out.println(temp);
                }
                input.close();
                }
             catch(IOException e) {
                System.err.println("Error on exec() method");
                e.printStackTrace();
            }
        }
    After compilation the exit value is 0 and creates a class file. But when i try to run it is either jumps to an exception and prints errors or prints the error from a BufferedReader:
    java.lang.NoClassDefFoundError: c:/Tem/Test1/MainTest
    Exception in thread "main"

    I have tried several way for cmd2 commands but just would not run.
    Any help would be much appriciated.

  2. #2
    Join Date
    Feb 2006
    Posts
    1
    I think you are missing the classpath to the newly generated .class file.

    try changing cmd2 to

    String[] cmd2 = {
    "c:/progra~1/Java/jdk1.5.0_05/bin/java.exe",
    "-cp",
    "c:/Temp/Test1",
    "MainTest" };

  3. #3
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    A simpler method is to include the tools.jar from the JDK distribution in the CLASSPATH and use the com.sun.tools.javac.Main class. Refer to Java Tip: Make a statement with javac!
    Happiness is good health and a bad memory.

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  2. Replies: 1
    Last Post: 09-05-2002, 09:01 AM
  3. Replies: 205
    Last Post: 09-26-2001, 01:37 PM
  4. How To Do It - Shared Class Variables Part III
    By Patrick Ireland in forum .NET
    Replies: 5
    Last Post: 05-10-2001, 06:19 PM
  5. Run a compiled class
    By Robert Levy in forum Java
    Replies: 6
    Last Post: 09-22-2000, 03:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center