wood burning stoves 2.0*
The moose likes Java in General and the fly likes javac api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "javac api" Watch "javac api" New topic
Author

javac api

Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
hi guys,

I plan to just build a small editor to just check the syntax of the java code. So i wanted to know how to make use of javac api for the same. If any one knows a good resource to make use or javac api in our application plese let me know.

note- i have gone through javacc.dev.java.net.. apart from that if there is any good site please let me know.

Thank you
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41600
    
  55
There isn't much documentation out there, but have a look at the section "PROGRAMMATIC INTERFACE" on this page.


Ping & DNS - my free Android networking tools app
Ranganath Junpal
Ranch Hand

Joined: Aug 31, 2006
Posts: 76
Its a good resource thank you. but please let me know if you are aware like can we leverge the power of syntax check like an interpreter in the editor as we see in ides like eclipse or netbeans using javac api?


Thank you!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41600
    
  55
No, javac does not make its parsing process visible through an API. But there are Java syntax descriptions for all the major parsers (JavaCC, SableCC, Antlr), on top of which you can build a syntax checker.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
JavaC is exposed in Java6. Look for Compiler class, I think.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Stan James:
JavaC is exposed in Java6. Look for Compiler class, I think.


Actually you want use the JavaCompiler interface. You get a reference to the system JavaCompiler by calling ToolProvider.getSystemJavaCompiler(). I was playing around with this stuff a couple of weeks ago, and here is the "magic incantation" that I uttered to compile a .java class file.

Now I'm sure this is probably not the most compact code for using the JavaCompiler, (the for loop at the end didn't do anything in my test, and I haven't tried it with a file with syntax errors to see how descriptive the Diagnostic messages are), but it does work.
[ January 23, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41600
    
  55
That looks complicated (although it probably does lots of things a direct invocation does not). Direct use of the class could be as simple as:



The documentation mentions that this particular class is considered OK to use, unlike the other com.sun... classes, which are not part of the public Java API.
[ January 24, 2007: Message edited by: Ulf Dittmer ]
Palash Sahu
Greenhorn

Joined: Dec 06, 2006
Posts: 13
Hi,

I am using jdk6 compiler api.Do I need to set tools.jar at the classpath.
Actually when I don't have tools.jar at classpath, I am getting NullPointerException at the bottom line..
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sjfm = compiler.getStandardFileManager(diagnostics, null, null);

Any help will be highly appreciated.

Regards,
Sabyasachi
Scott Kellicker
Greenhorn

Joined: Apr 03, 2007
Posts: 1
Sabyasachi,

I had a similar experience. getSystemJavaCompiler() returned null. I fixed this by running using the JDK instead of JRE. This link proved useful: https://openjdk.dev.java.net/compiler/guide/compilerAPI.html

ScottK


Originally posted by Palash Sahu:
Hi,

I am using jdk6 compiler api.Do I need to set tools.jar at the classpath.
Actually when I don't have tools.jar at classpath, I am getting NullPointerException at the bottom line..
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager sjfm = compiler.getStandardFileManager(diagnostics, null, null);

Any help will be highly appreciated.

Regards,
Sabyasachi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac api