• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

javac api

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaC is exposed in Java6. Look for Compiler class, I think.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic