Compiling And Running


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 15

Thread: Compiling And Running

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    151

    Question Compiling And Running

    Hi everyone,

    I am trying to compile some java codes from a .java file that i had saved to disk. Basically i am trying to build my own ide. I have a text area in which the java code is in and also have two buttons. What i need is when i click the button the .java file on my disk is compiled and any errors are shown in the textarea

    For the second button when i click it(if compilation is successfull) for the .java that has been compiled to be run.

    Does anyone know how to compile and run a java program from a program?

    I hope someone can help or show me some codings on how this can be achieved.

    Any amount of help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West

  2. #2
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    See

    Runtime.exec(String[]);

    and the Process class.

  3. #3
    Join Date
    Oct 2004
    Posts
    151

    Question

    hi everyone,

    I need to clear something with you guys

    let's say for example if my javac file is located in the file location
    "C://abc/fd/javac.exe"

    and the file i want to compile is located at "D://ac/d/lpc.java"

    so when i compile the file i must writethe following on the command line

    "C://abc/fd/javac D://ac/d/lpc.java"

    Am i right or wrong. If i am wrong then what is the correct way to do it

    Thank You

    Yours Sincerely

    Richard West

  4. #4
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    What I have done is pass something like this (Each string is an argument to the programs main() method):

    //remember to use double-backslash if you're on windows

    String command = new String{"C:\\abc\\fd\\javac", "D:\\ac\\d\\lpc.java"};

    Process p = Runtime.getRuntime.exec(command);

    //wait for p to finish, and display any messages if return code is negative, etc...

    hope that helps.

    If no, you could try passing "cmd", "/c" as the first to items of the String array. That works,too.

  5. #5
    Join Date
    Oct 2004
    Posts
    151
    Hi everyone,

    Doredson i tried what you said but it still does not work. I always get the exception null pointer even though it is not empty but the program compiles with no errors

    Here is part of the code

    Code:
    public void compile ()
    {
         byte[] buffer1 = new byte[2048];
         int len = 0;
        String str9 = "C:ddk1.4/bin/javac";
         String str10 = "D:gui.java";
         String[] str11 = {str9, str10};
      File File11 = new File("C://JIDE_Errors.TXT");
    
    try
    {
           Runtime1 = Runtime.getRuntime();
    //The below command command line is where the exception 
    //occurs saying it is a null pointer exception
    
            Process1 = Runtime1.exec(str11);
              InputStream in = new BufferedInputStream(Process1.getErrorStream());
            FileOutputStream out = new FileOutputStream(File11);
    
    while((len = in.read(buffer1)) != -1)
    {
              out.write(buffer1, 0, len);
    }
    
             Process1.waitFor();
    
    if(Process1.exitValue() == 0)
    {
                  System.out.println("Compilation Successful");
    }
    
    else
    {
    
    }
    
    }
    I tried the way you showed me but it still does not work. Am i missing something. I really do not understand why there is a null pointer exception.

    I hope someone can help me with this problem

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West

  6. #6
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Take a look at str9, str10. They don't have correct path information...I'll try pasting that into eclipse and doing something similar...

  7. #7
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Your method worked as expected once I fixed a couple of compile errors...

    I can't try exactly what you are doing, because I'm typing on an iMac right now. However, I did modify str9 to be "/bin/echo" and str10 to be "Hello World!", and as expected, the output file containted "Hello World!" after running.

    So, with that in mind, make sure the path to javac is correct(with correct path separator characters). Those strings are incorrect in what you posted. See comments in the code below.

    Here is the code (after I changed it to a main() method that I could run):

    Code:
      public static void main(String[] args) {
        byte[] buffer1 = new byte[2048];
        int len = 0;
        
        //This is missing a slash between C: and ddk1.4...should it be jdk4.1?
        //String str9 = "C:ddk1.4/bin/javac";
        //This worked on a Mac and Linux:
        String str9 = "/bin/echo";
        
        //Also missing a slash between D: and gui
        //Original String str10 = "D:gui.java";
        String str10 = "Hello World!";
        String[] str11 = { str9, str10 };
        
        //Again, you need a correct string here.  Either change the // to \\ or use 1 forward slash.
        //Original: File File11 = new File("C://JIDE_Errors.TXT");
        File File11 = new File("myfile.txt");
    
        try {
          Runtime Runtime1 = Runtime.getRuntime();
    
          Process Process1 = Runtime1.exec(str11);
          BufferedInputStream in = new BufferedInputStream(Process1.getInputStream());
          FileOutputStream out = new FileOutputStream(File11);
    
          while ((len = in.read(buffer1)) != -1) {
            out.write(buffer1, 0, len);
          }
    
          Process1.waitFor();
    
          if (Process1.exitValue() == 0) {
            System.out.println("Compilation Successful");
          }
    
          else {
    
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    cheers.

  8. #8
    Join Date
    Oct 2004
    Posts
    151

    Question

    Hi everyone,

    Sorry to bother you doredson but what if i want to run the class file that i have already compiled.

    Let's say if my compiled class is located at C:/JButtons.class
    and if i want to run this class this is what i did

    Code:
        String[] str12 ={"C:/j2sdk1.4.2_04/bin/java", "C:/JButtons"}; 
    
          Runtime Runtime1;
          Process Process1;
    
    try
    {
           Runtime1 = Runtime.getRuntime();
           Process1 = Runtime1.exec(str12);
    
           Process1.waitFor();
    }
    
    catch(Exception e)
    {
           e.printStackTrace();
    }
    There is no exception thrown but the exit value of the process is 1 (meaning the program exited abnormally). I do not know what i am doing wrong. Am i running the compiled class the correct way. If not then what is the correct way of doing it. The program i trying to run has no errors when compiled. Basically what i need is a way to change my class path programatically.

    I hope some someone can help me with this problem

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
    Last edited by freesoft_2000; 12-12-2004 at 02:25 PM.

  9. #9
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    try printing out the messages you get, as in the earlier example, send the output to System.out, or something.

  10. #10
    Join Date
    Oct 2004
    Posts
    151

    Question

    hi everyone,

    I already did that but the compiled class is not run but the program exits abnormally and there are no exceptions. I am thinking it has something to do with the classpath but i am not sure if i am setting it correctly in the above code snippet. I also tried this( as an attempt to change the classpath)

    Code:
          String[] str12 ={Rstr, "-cp.;", "C:/JButtons"}; 
    
    // Rstr is the location of my java intrepreter
    // The value of Rstr is C:/j2sdk1.4.2_04/bin/java
    // C:/JButtons is the location of the class i want to run but can't
    // seem to although the class exists at that location
    
           Runtime Runtime1;
           Process Process1;
    
    try
    {
           Runtime1 = Runtime.getRuntime();
           Process1 = Runtime1.exec(str12);
    
           Process1.waitFor();
    }
    
    catch(Exception e)
    {
           e.printStackTrace();
    }
    What i think is that the problem has to do with the setting of my arguments in the str12 array and i maybe my setting of the class path is wrong but i am very sure of setting the classpath correctly
    programatically

    I hope someone can help me with this problem

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
    Last edited by freesoft_2000; 12-13-2004 at 03:51 AM.

  11. #11
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Separate the -cp and the actual value for the classpath:

    "-cp", "."

    Also, classpath will use the colon separator on non-windoze platforms, fyi.

    You should not have the full path information of the class file you want to run, because it is supposed to be found on your classpath, so instead of "C:/JButtons", you should have "JButtons".

    good luck!

  12. #12
    Join Date
    Oct 2004
    Posts
    151

    Question

    Hi everyone,

    i am really so sorry for bothering you doredson but this is what i did

    Code:
    String[] str12 ={Rstr,
                            
                              "-cp" ,
    
                               "C:/" ,
      
                                 ";"  ,                    
     
                                 "."  ,
    
                                "JButtons"}; 
    
    // Rstr is the location of my java intrepreter
    // The value of Rstr is C:/j2sdk1.4.2_04/bin/java
    // C:/JButtons is the location of the class i want to run but can't
    // seem to although the class exists at that location
    
           Runtime Runtime1;
           Process Process1;
    
    try
    {
           Runtime1 = Runtime.getRuntime();
           Process1 = Runtime1.exec(str12);
    
           Process1.waitFor();
    }
    
    catch(Exception e)
    {
           e.printStackTrace();
    }
    I tried running the class but still the class is not run. Are the arguments for my string correct. If not then what is the correct way. Am i missing something?

    Once again so sorry for disturbing you like that doredson

    I hope someone can help me with this problem

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
    Last edited by freesoft_2000; 12-13-2004 at 12:06 PM.

  13. #13
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    Put your entire classpath as a single string.

    "-cp", ".;c:/"

  14. #14
    Join Date
    Nov 2004
    Location
    Minnesota
    Posts
    99
    FYI, the reason this will not work:
    String[] str12 ={Rstr,"-cp" ,"C:/" ,";" ,"." , "JButtons"};

    is the same reason that this will not work on the command line:

    doesn't work(has spaces):
    java -cp c:\ ; . JButtons

    works (no spaces):
    java -cp "c:\;." JButtons


    each string is an argument to the binary, "java" and java -h says the argument to -cp should be a single string, not multiple strings

    hope that clarifies.

  15. #15
    Join Date
    Oct 2004
    Posts
    151

    Talking

    Hi everyone,

    I finally got it working but i could not have done without your help so let me say thank you doredson so much for help in helping me get my concepts correct.

    Thank You

    Yours Sincerely

    Richard West
    Last edited by freesoft_2000; 12-15-2004 at 03:11 PM.

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