This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Compiling Java with Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling Java with Java" Watch "Compiling Java with Java" New topic
Author

Compiling Java with Java

cardwell cupp
Ranch Hand

Joined: Aug 16, 2001
Posts: 66
Are there any functions that compile a java source file?
Are there any functions taht can make system calls?
(i.e. c++ has system("command"))
Thanks
Cardwell
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yes.
Yes.
OH! You want specifics.
You would need to compile to a specific platform. Do a google search using +java+compilers and you will find lots.
Actually the JRE uses a JIT compiler (Just in time) which compiles the class files just before using them, so there is really not alot of benefit to a separate compiler. I don't think that there is much performance gained, and it is alot easier to invoke an application using a .bat file instead of an exe. But if you insist.
You can use Runtime.
Try
Runtime.getRuntime().exec("cmd /K del File1.txt");
(the older version of windows use the word "command")
You can also do:
Runtime.getRuntime().exec("cmd /K start run.bat");
and
Runtime.getRuntime().exec(notepad.exe);

"JavaRanch, where the deer and the Certified play" - David O'Meara
cardwell cupp
Ranch Hand

Joined: Aug 16, 2001
Posts: 66
Thanks tons for you input.
Correct me if I am wrong, the code at the bottom of your reply just runs a batch file (in which I could set up javac Whatever.java) right?
Is it possible to run javac with out a batch file? Is there a function that runs javac on a certain specified file??
(i.e)
public void javac(String fileToBeCompiled)

Thanks for the help!
Cardwell(getting greener all the while)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I think that you can just include the parameter with the call to the executable. Course then you need to put it in quotes.
Runtime.getRuntime().exec("javac.exe MyClass.java");
cardwell cupp
Ranch Hand

Joined: Aug 16, 2001
Posts: 66
Thanks tons!!
Cardwell Cupp(not soo green now!)
Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Hi
Originally posted by cardwell cupp:
public void javac(String fileToBeCompiled)
[/B]

In the JDK 1.1.8 is a class sun.tools.javac.Main for compiling to bytecode. Just call sun.tools.javac.Main.main("fileToBeCompiled.java").
I don't know if there is this class in other JDK's or JRE's
Helmut
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling Java with Java
 
Similar Threads
how to use native keyword in java?
Overloaded functions in oracle
virtual functions
How do i call my function in JSP
invoking vb dll functions in java