Results 1 to 10 of 10
- 07-11-2010, 03:35 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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:
in this code i write a very simple program defined by a string, to a file and then compile and run it.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(); } }
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 :
in the above example "first" "first" is printed but it is intended to print "first" "second"!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(); } }
do you have any idea to address this problem?
Thanks in advance,
Z.Zojaji
- 07-11-2010, 03:43 PM #2
Where do you close the files?
- 07-11-2010, 05:13 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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] } }
- 07-11-2010, 05:27 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 07-11-2010, 05:53 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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:
any alternative solution?Java Code:ClassLoader cl = ToolProvider.getSystemToolClassLoader(); try { cl.loadClass("JavaProgram0"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
- 07-11-2010, 06:11 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 07-12-2010, 12:01 AM #7
Where is JavaProgram0 defined that you can compile this?JavaProgram0 j = new JavaProgram0();
- 07-12-2010, 06:36 AM #8
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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.Where is JavaProgram0 defined that you can compile this?
- 07-12-2010, 08:45 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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
- 07-15-2010, 07:06 AM #10
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
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:
How to compile and execute a Java file from within another Java Program | Rommel RicoJava 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);
Similar Threads
-
How to compile java program in other than bin directory
By amritpalpathak in forum New To JavaReplies: 3Last Post: 06-10-2010, 03:14 PM -
Teach Me Pls How To Compile Java Program Using CMD
By Lilsimple in forum Java AppletsReplies: 1Last Post: 03-11-2010, 04:26 PM -
How can I compile and execute the Java program in a non-C drive (D or E)?
By tyang in forum New To JavaReplies: 11Last Post: 02-08-2010, 12:15 AM -
My Java program will not compile and run?
By sabrown311313 in forum Java AppletsReplies: 1Last Post: 09-23-2008, 09:38 AM -
Program Won't Compile
By JavaLovenJoe in forum New To JavaReplies: 2Last Post: 04-22-2008, 01:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks