• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

programatically compile

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would any one please tell me that if i want to compile "abc.txt"(in which i have written simple java code) from my java programe(say TestClass) how can i do this??? if someone knows the code please do paste in reply......i am waiting for your response......

regards:
Amir Iqbal
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you store the code in .java file on disk? If so, you can use the javac compiler internally, somewhat like this:



The first parameter is an array of absolute paths of Java source files, the second is where error messages should go, so you might use "new PrintWriter(System.err)" for it. It returns 0 if compilation worked fine, some other int if not.

If it all needs to be in memory then it gets a bit trickier. I described how it can work in this recent article.

Another approach would be to drive javac using Runtime.exec. Which method is best depends on what you intend to do with the resulting class.
[ November 10, 2007: Message edited by: Ulf Dittmer ]
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also check out these examples from java2s.com. Please note that you have to run it from the java.exe from the JDK bin folder, otherwise you'll get a null Java Compiler object.
 
Amir Iqbal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks alot my frndz Ulf and Rob and everyone who paid attention.
thank you very much.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
Can you store the code in .java file on disk? If so, you can use the javac compiler internally, somewhat like this:


Note that if you do this, you are using an internal Sun package, that you're not really supposed to use. See this: Why Developers Should Not Write Programs That Call 'sun' Packages.

Since Java 6, there is an official API to call the Java compiler from your program. It is in the package javax.tools. Lookup the API documentation for that package.
 
BWA HA HA HA HA HA HA! Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic