Results 1 to 19 of 19
- 03-08-2013, 01:44 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 15
- Rep Power
- 0
- 03-08-2013, 01:46 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 688
- Rep Power
- 1
Re: Compiling another java code from our code
I haven't see the first solution.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-08-2013, 01:57 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 15
- Rep Power
- 0
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(); }
}
}
- 03-08-2013, 02:19 PM #4
Re: Compiling another java code from our code
Why do they call it rush hour when nothing moves? - Robin Williams
- 03-08-2013, 02:27 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 688
- Rep Power
- 1
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.
Regards,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(); } } }
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-08-2013, 02:29 PM #6
Re: Compiling another java code from our code
... and When Runtime.exec() won't
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-08-2013, 03:08 PM #7
Member
- Join Date
- Jan 2013
- Posts
- 15
- Rep Power
- 0
Re: Compiling another java code from our code
dir command works completely fine but javac dont..why??
- 03-08-2013, 03:36 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 688
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-08-2013, 03:48 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-08-2013, 03:56 PM #10
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
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())); } } }
- 03-08-2013, 04:41 PM #11
Member
- Join Date
- Jan 2013
- Posts
- 15
- Rep Power
- 0
Re: Compiling another java code from our code
thanx now i m trying to run the compiled file..
- 03-08-2013, 04:47 PM #12
Member
- Join Date
- Jan 2013
- Posts
- 15
- Rep Power
- 0
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?
- 03-08-2013, 04:54 PM #13
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 688
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-08-2013, 05:28 PM #14
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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:
and call it from your code by doing: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())); } } }
might be a "saner" approach than trying to integrate batch files to call other files to call other files, etc...Java Code:Compile compile = new Compile("/path/to/your/source/file.java"); compile.doCompile()Last edited by SnakeDoc; 03-08-2013 at 05:34 PM.
- 03-10-2013, 02:04 PM #15
Member
- Join Date
- Jan 2013
- Posts
- 15
- Rep Power
- 0
Re: Compiling another java code from our code
i still cannot run my compiled file...please help..... :(
- 03-11-2013, 10:09 AM #16
Godlike
- Join Date
- Nov 2012
- Posts
- 195
- Rep Power
- 1
Re: Compiling another java code from our code
What code do you use to run the class file?
- 03-11-2013, 11:39 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 03-11-2013, 04:11 PM #18
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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 04:13 PM.
- 03-11-2013, 04:54 PM #19
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Java code not compiling!
By tanveerahmed in forum New To JavaReplies: 3Last Post: 11-25-2012, 09:33 PM -
Issue compiling Java Code
By AggressiveFish in forum Java AppletsReplies: 4Last Post: 01-04-2011, 09:10 PM -
Compiling java code from the command line
By alman9898 in forum New To JavaReplies: 5Last Post: 10-13-2010, 03:35 AM -
my code compiling but not running
By girishkumar in forum New To JavaReplies: 16Last Post: 03-16-2010, 04:45 PM -
Trouble compiling code
By waelhelbawi in forum New To JavaReplies: 1Last Post: 05-12-2008, 04:25 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks