Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By SnakeDoc

Thread: Compiling another java code from our code

  1. #1
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Compiling another java code from our code

    I need to compile a java code and print the output to a text file .
    If the file gets compiled successfully then run that class file.

    I have tried using Process class but does not work..Any other solution???

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Compiling another java code from our code

    I haven't see the first solution.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Compiling another java code from our code

    here is the code i tried

    import java.util.*;
    import java.io.*;
    public class BadExecJavac
    {
    public static void main(String args[])
    { try
    {
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("cmd/c javac "+path address of file to be compiled);

    }
    catch (Throwable t) { t.printStackTrace(); }
    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Compiling another java code from our code

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Compiling another java code from our code

    I didn't try to compile anything but this worked as expected. Looks like it could be a path environment issue. Make certain you use complete pathnames.

    Java Code:
    public class BadExecJavac {
       public static void main(String args[]) {
          try {
             Runtime rt = Runtime.getRuntime();
             Process proc = rt.exec("C:/WINDOWS/system32/cmd.exe /c dir > \"C:/Documents and Settings/user/Desktop/dir.txt\"");
    
          }
          catch (Throwable t) {
             t.printStackTrace();
          }
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Compiling another java code from our code

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Compiling another java code from our code

    dir command works completely fine but javac dont..why??

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Compiling another java code from our code

    I don't know but I am interested enough to keep trying. And normally I don't ask why someone wants to do something but in this case I am curious as there are much better ways to control compilation of source code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Compiling another java code from our code

    What does "does not work" mean?
    You're not doing anything with the either the error stream or the standard output stream, so you're not going to see any errors coming out of the execution.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Compiling another java code from our code

    Here's code to compile it using the JavaCompiler API. It's *very* quick and dirty, but it does the job. I am not sure how to run it using pure Java code.
    Java Code:
    package bla;
    
    import javax.tools.JavaCompiler;
    import javax.tools.ToolProvider;
    import java.io.ByteArrayOutputStream;
    
    public class Compile {
    
        public static void main(String[] args) {
            String fileToCompile = "/home/username/MyFile.java";
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    
            ByteArrayOutputStream os = new ByteArrayOutputStream();
    
            int compilationResult = compiler.run(null, null, os, fileToCompile);
            if (compilationResult == 0) {
                System.out.println("Compilation is successful");
            }
            else {
                System.out.println("Compilation Failed");
                System.out.println(new String(os.toByteArray()));
            }
        }
    
    }

  11. #11
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Compiling another java code from our code

    thanx now i m trying to run the compiled file..

  12. #12
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Compiling another java code from our code

    should i make that file a batch file....so that my code can call it??any suggestions?

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Compiling another java code from our code

    What?!? Are you suggesting that you want your code to call SurfMan's code to compile a program?

    Why don't you just use ANT?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  14. #14
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Compiling another java code from our code

    Ant might be overkill for a small project or if he's just learning... although it is the accepted method to compile code automagically...

    maybe try a constuctor to initialize the Compile class with the path to your java source you want compiled. Then in your code, you can just call:

    so modify the sample to:

    Java Code:
    package bla;
     
    import javax.tools.JavaCompiler;
    import javax.tools.ToolProvider;
    import java.io.ByteArrayOutputStream;
     
    public class Compile {
    
        private String path;
    
        public Compile(String path) { // constuctor initializes to set file path
            this.path = path;
        }
        public Compile() {  // empty constructor
        }
    
        public void setCompilePath(String path) {  // set file path
            this.path = path;
        }
        public String getCompilePath() {  // get file path
            return this.path;
        }
     
        public void doCompile() {  // compile file located at path
    
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
     
            ByteArrayOutputStream os = new ByteArrayOutputStream();
     
            int compilationResult = compiler.run(null, null, os, this.path;
            if (compilationResult == 0) {
                System.out.println("Compilation is successful");
            }
            else {
                System.out.println("Compilation Failed");
                System.out.println(new String(os.toByteArray()));
            }
        }
     
    }
    and call it from your code by doing:

    Java Code:
    Compile compile = new Compile("/path/to/your/source/file.java");
    compile.doCompile()
    might be a "saner" approach than trying to integrate batch files to call other files to call other files, etc...
    Last edited by SnakeDoc; 03-08-2013 at 06:34 PM.
    SurfMan likes this.

  15. #15
    madhav.dubey9 is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Blog Entries
    1
    Rep Power
    0

    Default Re: Compiling another java code from our code

    i still cannot run my compiled file...please help..... :(

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,031
    Rep Power
    4

    Default Re: Compiling another java code from our code

    What code do you use to run the class file?

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Compiling another java code from our code

    Have you tried to actually compile that file "by hand" (ie from the command line)?
    In other words, does it actually compile?

    If so, then what happens when you try to run it.

    "It does not work" is no use to us in resolving your problem. We cannot see your computer from here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default Re: Compiling another java code from our code

    if you really insist on going this route to run other java programs... read up on Runtime.getRuntime().exec() :

    Runtime (Java 2 Platform SE v1.4.2)
    Last edited by SnakeDoc; 03-11-2013 at 05:13 PM.

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Compiling another java code from our code

    ... or load the class using a ClassLoader and invoke the main(...) method using reflection. If you have enough knowledge and experience to go down that path, my File Class Loader may be useful.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java code not compiling!
    By tanveerahmed in forum New To Java
    Replies: 3
    Last Post: 11-25-2012, 10:33 PM
  2. Issue compiling Java Code
    By AggressiveFish in forum Java Applets
    Replies: 4
    Last Post: 01-04-2011, 10:10 PM
  3. Compiling java code from the command line
    By alman9898 in forum New To Java
    Replies: 5
    Last Post: 10-13-2010, 04:35 AM
  4. my code compiling but not running
    By girishkumar in forum New To Java
    Replies: 16
    Last Post: 03-16-2010, 05:45 PM
  5. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 05:25 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
  •