Executing Eclipse plugin (jdt/ast) outside eclipse IDE environment

给你一囗甜甜゛ 提交于 2019-12-10 10:19:01

问题


Can I execute Eclipse Java development tools as a standalone process? I need to get Java program's AST structure using Eclipse JDT from another program, and to do that, I need to execute eclipse plugin as a stand-alone process behind the scene.

Is that possible? If so, how one can do that?


回答1:


Eclipse/jdt and eclipse/ast is nothing more than a jar file, so one can use them to build standalone java application.

Example

I googled to find ASTExplorer as an example to make eclipse/ast plugin as a standalone java project.

The program was targeted for pretty old eclipse (3.0.2), I downloaded the 3.0.2 for Mac OS X this site - http://archive.eclipse.org/eclipse/downloads/drops/R-3.0.2-200503110845/

  1. You need to setup ECLIPSE_HOME classpath variables in Preference -> Java -> Classpath variables

  2. The .classpath has those classpath variables already, you can refer to this post - .classpath contents update in eclipse. As you can see it refers eclipse/jdt(art) jar files. I'm pretty sure one can change the reference to the newest version of jdt/ast without downloading the 3.0.2 version.
  3. As I use Mac, I need to replace the swt for PC with swt for Mac. I could do that in BuildPath/Configure Build path

    1. First remove the PC swt reference.
    2. Then add the correct Mac swt reference.

Compile the example with the eclipse indigo/on mac

When the setup is correct, eclipse starts building the project. You can use Project -> Build Project menu. Then, you can check the application works fine in eclipse.

Generate the executable jar

  1. Export to executable jar file.

  2. You'll get some warnings, but you'll have a jar file.

Execute the generated jar

Just executing java -jar ast.jar doesn't work on Mac, I got a hint from this post. Running SWT based, cross-platform jar properly on a Mac

In short, you need to run java -XstartOnFirstThread -jar ast.jar




回答2:


Does it have to be Eclipses's AST? Or is any AST generation o.k.? If it doesn't have to be eclipse, I'd suggest Habelitz open source Java AST Compiler. http://www.habelitz.com/ I'm using it myself for some projects....

Well, of course you can launch eclipse just like any other process : How to create a process in Java

And then the problem becomes relaying the information back to the original process, which gets kind of messy. My suggested approach in this case would probably be to create a plugin in eclipse and have that plugin do whatever it is you think you need the AST for. You can work out messages with command line options to eclipse, or a shared file or something.



来源:https://stackoverflow.com/questions/12534012/executing-eclipse-plugin-jdt-ast-outside-eclipse-ide-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!