Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Tracking JDT Compiler Error Messages (IProblem)(Accessing Workspace Resources Within Standalone Java Application)
Tracking JDT Compiler Error Messages (IProblem) [message #1052865] Tue, 30 April 2013 19:27 Go to next message
David Boulanger is currently offline David BoulangerFriend
Messages: 5
Registered: April 2013
Junior Member
Hello,

I am currently developing programming tutors. Those tutors are standalone Java applications. I use Eclipse JDT to parse Java source code and build the corresponding abstract syntax tree. Moreover, I want to capture the error messages that would be launched if a student would compile his code within Eclipse. Eclipse enables us to parse and track error messages for a specific string of code (without context). After having "compiled" that string, I got 9 error messages. When I pasted that code within an Eclipse project, I got around 90 error messages. Obviously, the purpose is to track as much errors as possible. I suspect that it depends on the context of the source code.

For that purpose, I tried to create programmatically a project within the workspace and create a file containing the source code string. However, when executing my Java application I get the following error message:

Exception in thread "main" java.lang.IllegalStateException: Workspace is closed.
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:399)
at com.example.helloworld.JavaErr.main(JavaErr.java:67)

I read many articles on this topic. It seems like we can access resources within the workspace only within an Eclipse plugin project. Before giving up that "solution" path, would it be possible to know if we can access the workspace and all its resources programmatically within an external standalone Java application?

Thanks for your help.

David
Re: Tracking JDT Compiler Error Messages (IProblem) [message #1052934 is a reply to message #1052865] Wed, 01 May 2013 13:20 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
First, if AST creation yields fewer problems than you expect, have you checked if these problems are
- configurable problems that just need to be enabled via the options used by the ASTParser?
- reported against secondary files, i.e., not the being passed to the parser? (Have you seen ASTParser.createASTs(..) - the plural version?)
- problems that are not necessarily detected by parsing alone (i.e., semantic problems that may be detected as late as during code generation)? Does requesting resolved bindings make a difference?


That said, using the concept of Java projects indeed depends on a workspace. Note, that this doesn't necessarily require to fire up the entire IDE, it's not the workbench that's needed but a workspace. The latter can in principle be initialized in a standalone application, too, but I don't have a HowTo at hand for this scenario.

HTH,
Stephan
Re: Tracking JDT Compiler Error Messages (IProblem) [message #1052936 is a reply to message #1052865] Wed, 01 May 2013 13:30 Go to previous messageGo to next message
David Boulanger is currently offline David BoulangerFriend
Messages: 5
Registered: April 2013
Junior Member
Thanks, Stephan, for your quick reply and very helpful post. I will delve more into this today. I will let you know if I succeed to solve my problem.

David
Re: Tracking JDT Compiler Error Messages (IProblem) [message #1052975 is a reply to message #1052936] Wed, 01 May 2013 16:57 Go to previous messageGo to next message
David Boulanger is currently offline David BoulangerFriend
Messages: 5
Registered: April 2013
Junior Member
Hello Stephan,

Just to let you know that your third hint brings me to the solution I sought. I needed finally to resolve the bindings between the char[] source code and its environment (setEnvironment) and set the name of the compilation unit. Setting the environment enables us to bypass the workspace and create our own "virtual" project by importing the proper libraries (classpath) and the source code (sourcepath). If somebody would like to retrieve also syntax errors, he has only to enable the recovery of statements. Thus we are able to parse an external Java file in a standalone Java application and retrieve the same amount of errors as it would in a real Eclipse project (except for the package declaration error). Here is the code fragment I used:

File file = new File("");
String srcCode = Files.toString(file, Charsets.UTF_8);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(srcCode.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
parser.setCompilerOptions(options);

String[] classpath = {"", "", ""};
String[] sourcepath = {""};

parser.setEnvironment(classpath, sourcepath, null, true);
parser.setUnitName("");
parser.setResolveBindings(true);
parser.setBindingsRecovery(true);
parser.setStatementsRecovery(true);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor2(cu));

Again, thanks for having taken the time to provide me with those hints. It is really appreciated.
Re: Tracking JDT Compiler Error Messages (IProblem) [message #1052994 is a reply to message #1052975] Wed, 01 May 2013 19:00 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
I'm glad it helped Smile
Previous Topic:missing tomcat runtime environment
Next Topic:Xhtml page editor something rong
Goto Forum:
  


Current Time: Fri Mar 29 14:30:04 GMT 2024

Powered by FUDForum. Page generated in 0.03118 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top