Results 1 to 10 of 10
  1. #1
    z.zojaji is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default Compile and run a java program through another one

    Hi all,
    I want to compile and run some run-time generated java code via another one, consider for example the following program:

    Java Code:
    public class Test {
    	public static void main(String[] args) throws IOException {
    		 FileOutputStream fos = new FileOutputStream("src/JavaProgram0.java");
    	        String s = "\npublic class JavaProgram0 {\n" +
    	        		"	public void print() {System.out.println(\"first\");\n	}\n}";
    	        fos.write(s.getBytes());
    	        fos.close();
    	        
    		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    		int result = compiler.run(null, null, null,"src/JavaProgram0.java");
            JavaProgram0 j = new JavaProgram0();
            j.print();
            
        }
    }
    in this code i write a very simple program defined by a string, to a file and then compile and run it.
    it is Ok for one program but if i extend it to compile and run more than one programs it just runs the first one :

    Java Code:
    public class Test {
    	public static void main(String[] args) throws IOException {
    		 FileOutputStream fos = new FileOutputStream("src/JavaProgram0.java");
    	        String s = "\npublic class JavaProgram0 {\n" +
    	        		"	public void print() {System.out.println(\"first\");\n	}\n}";
    	        fos.write(s.getBytes());
    	        fos.close();
    	        
    		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    		int result = compiler.run(null, null, null,"src/JavaProgram0.java");
            JavaProgram0 j = new JavaProgram0();
            j.print();
    
        FileOutputStream fos1 = new FileOutputStream("src/JavaProgram0.java");
            String s1 = "\npublic class JavaProgram0 {\n" +
            		"	public void print() {System.out.println(\"second\");\n	}\n}";
            fos1.write(s1.getBytes());
            
            result = compiler.run(null, null, null,"src/JavaProgram0.java");
            JavaProgram0 j2 = new JavaProgram0();
            j2.print();
        }
    }
    in the above example "first" "first" is printed but it is intended to print "first" "second"!
    do you have any idea to address this problem?
    Thanks in advance,
    Z.Zojaji

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    Where do you close the files?

  3. #3
    z.zojaji is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    in the mentioned code i close just the first one (as a mistake) but if I close each after writing and before compile, it will print "second " "second", the code will be as follow:
    Java Code:
    public class Test {
    	public static void main(String[] args) throws IOException {
    		 FileOutputStream fos = new FileOutputStream("src/JavaProgram0.java");
    	        String s = "\npublic class JavaProgram0 {\n" +
    	        		"	public void print() {System.out.println(\"first\");\n	}\n}";
    	        fos.write(s.getBytes());
    [COLOR="Red"]	        fos.close();[/COLOR]
    	        
    		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    		int result = compiler.run(null, null, null,"src/JavaProgram0.java");
                    JavaProgram0 j = new JavaProgram0();
                    j.print();
    
        [INDENT]           FileOutputStream fos1 = new FileOutputStream("src/JavaProgram0.java");
            String s1 = "\npublic class JavaProgram0 {\n" +
            		"	public void print() {System.out.println(\"second\");\n	}\n}";
            fos1.write(s1.getBytes());
            
            result = compiler.run(null, null, null,"src/JavaProgram0.java");
            JavaProgram0 j2 = new JavaProgram0();
            j2.print();
    [COLOR="Red"][B]        fos1.close();[/B][/COLOR][/INDENT]
        }
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    What happens if you throw away your JavaProgram0.class file and run again? It won't solve your problem because you're fighting against the ClassLoader (that has already loaded and cached your class after loading it the first time) but start with a clean list first.

    kind regards,

    Jos

  5. #5
    z.zojaji is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Dear Jos , my opinion is like you my original application generates hundreds of programs automatically and .java file is correctly updated and compiled but all af them run the first .class file. so I add the following code after compile in order to reload the .class files but the problem is not solved yet:

    Java Code:
    	ClassLoader cl = ToolProvider.getSystemToolClassLoader();
    		try {
    			cl.loadClass("JavaProgram0");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    any alternative solution?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by z.zojaji View Post
    Dear Jos , my opinion is like you my original application generates hundreds of programs automatically and .java file is correctly updated and compiled but all af them run the first .class file. so I add the following code after compile in order to reload the .class files but the problem is not solved yet:

    Java Code:
    	ClassLoader cl = ToolProvider.getSystemToolClassLoader();
    		try {
    			cl.loadClass("JavaProgram0");
    		} catch (ClassNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    any alternative solution?
    Class loaders cache the classes they have loaded so you have to write your own non-caching class loader for that purpose. I can't help you there because I've never done it before. It seems quite an achievement though and all I know of is a "rebel class loader" (google for that) that can do it. I wish you success with this project.

    kind regards,

    Jos

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    JavaProgram0 j = new JavaProgram0();
    Where is JavaProgram0 defined that you can compile this?

  8. #8
    z.zojaji is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Where is JavaProgram0 defined that you can compile this?
    JavaProgram0 should be generated and updated in run-time but i initialized it to a java class with an empty "print" method to avoid errors.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    If someone spams a forum I appreciate it if it is spammed correctly, i.e. readable text, fancy pictures here and there (if applicable); but this looks just crappy; the spammer can't even get its character set right; reported.

    Jos

  10. #10
    z.zojaji is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Hi all,
    I've finally found an alternative solution for the problem, after generating new .class files I run them using "exec()" method and assign the output of process to a bufferdReader for further use, it is some thing like this:

    Java Code:
                Runtime rt = Runtime.getRuntime();
                Process pr = rt.exec("java "+outFile);
                BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                String line=null;
                while((line=input.readLine()) != null) {
                    System.out.println(line);
    How to compile and execute a Java file from within another Java Program | Rommel Rico

Similar Threads

  1. How to compile java program in other than bin directory
    By amritpalpathak in forum New To Java
    Replies: 3
    Last Post: 06-10-2010, 03:14 PM
  2. Teach Me Pls How To Compile Java Program Using CMD
    By Lilsimple in forum Java Applets
    Replies: 1
    Last Post: 03-11-2010, 04:26 PM
  3. Replies: 11
    Last Post: 02-08-2010, 12:15 AM
  4. My Java program will not compile and run?
    By sabrown311313 in forum Java Applets
    Replies: 1
    Last Post: 09-23-2008, 09:38 AM
  5. Program Won't Compile
    By JavaLovenJoe in forum New To Java
    Replies: 2
    Last Post: 04-22-2008, 01:31 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
  •