This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac api

 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't much documentation out there, but have a look at the section "PROGRAMMATIC INTERFACE" on this page.
 
Ranganath Junpal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JavaC is exposed in Java6. Look for Compiler class, I think.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic