Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » ASTParser setEnvironment classpath problems
ASTParser setEnvironment classpath problems [message #1064051] Mon, 17 June 2013 12:40 Go to next message
Francois Dufault is currently offline Francois DufaultFriend
Messages: 2
Registered: June 2013
Junior Member
Hello everyone.

I readed blogs and tutorials to activate bindings on the JDT AST tree with "external" project (not eclipse plugin). But I have some difficulties with the classpath when passing the setEnvironment to the parser.

This is the simple class that I want to parse:
import org.apache.log4j.Logger;

public class LogTest {
	private static Logger LOG = Logger.getLogger(LogTest.class);
}


But when I parse this code with ASTParser, I have those compilations problems:
Pb(390) The import org.apache cannot be resolved
Pb(2) Logger cannot be resolved to a type
Pb(50) Logger cannot be resolved


So, by those errors, the log4j jar library was not found by the compiler.

Here the method I used to load/parse the AST tree:
public static void testJDTParser() {
		String javaFile = "C:\\workspace\\BasicProjet\\src\\LogTest.java";
		ASTParser parser = ASTParser.newParser(AST.JLS4);

		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		char[] content = ... //LOADING FILE javaFile INTO CHAR[].
		parser.setSource(content);

		@SuppressWarnings("unchecked")
		Map<String, String> options = JavaCore.getOptions();
		options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
		options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
				JavaCore.VERSION_1_6);
		options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);

		String unitName = "BasicProjet/LogTest.java";
		parser.setUnitName(unitName);

		String[] sources = { "C:\\workspace\\BasicProjet" };
		String[] classPaths = { "C:\\workspace\\BasicProjet\\lib" };

		parser.setEnvironment(classPaths, sources,
				new String[] { "UTF-8" }, true);
		parser.setBindingsRecovery(true);
		parser.setResolveBindings(true);
		parser.setCompilerOptions(options);
		parser.setStatementsRecovery(true);

		CompilationUnit result = (CompilationUnit) parser.createAST(null);

		IProblem[] problems = result.getProblems();

		for (IProblem problem : problems) {
			LOG.debug(problem.toString());
		}

		if (result.getAST().hasBindingsRecovery()) {
			LOG.warn("Binding activated.");
		}
                //Get the "LOG" field of the class LogTest parsed.
		TypeDeclaration typeDeclaration = 
                       (TypeDeclaration)result.types().get(0);
		FieldDeclaration field = typeDeclaration.getFields()[0];

		ITypeBinding binding = field.getType().resolveBinding();
                //binding is null here.
		LOG.debug("..");
}


The bindings are activated, the message "Binding activated." show up in the console.
The paths are valid. The librairy log4j is in the "lib" directory.
I tried with other librairies (JUnit, commons-io, commons-lang, etc) with the same result.

Dev environment:
Windows XP SP3 (32 bits)
Eclipse JDT Core 3.8.3

Librairies in buildpath of the project containing this testJDTParser() method:
<classpathentry kind="lib" path="lib/org.eclipse.jdt.core_3.8.3.v20130121-145325.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.core.runtime_3.8.0.v20120521-2346.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.equinox.common_3.6.100.v20120522-1841.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.core.resources_3.8.1.v20121114-124432.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.core.jobs_3.5.200.v20120521-2346.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.osgi_3.8.2.v20130124-134944.jar" sourcepath="lib/org.eclipse.osgi.source_3.8.2.v20130124-134944.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.core.contenttype_3.4.200.v20120523-2004.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.osgi.services_3.3.100.v20120522-1822.jar" sourcepath="lib/org.eclipse.osgi.services.source_3.3.100.v20120522-1822.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.osgi.util_3.2.300.v20120913-144807.jar"/>
	<classpathentry kind="lib" path="lib/org.eclipse.equinox.preferences_3.5.1.v20121031-182809.jar"/>


How can I tell the ASTParser where to find the external librairies ?

Note: I can't use the IProject/IWorkspace, i'm not using a Eclipse Plugin.

Thank you.
Francois.
Re: ASTParser setEnvironment classpath problems [message #1064101 is a reply to message #1064051] Mon, 17 June 2013 15:56 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
You need to set the classpaths to include all the jars you are interested in. Don't use:
String[] classPaths = { "C:\\workspace\\BasicProjet\\lib" };
but list explicitly all the jars you want in this folder:
String[] classPaths = { "C:\\workspace\\BasicProjet\\lib\\log4j.jar", ... };

HTH,

Olivier
Re: ASTParser setEnvironment classpath problems [message #1064113 is a reply to message #1064101] Mon, 17 June 2013 17:16 Go to previous messageGo to next message
Francois Dufault is currently offline Francois DufaultFriend
Messages: 2
Registered: June 2013
Junior Member
Ho !

That's why ! It works now.

Thank you, Olivier.

Francois.
Re: ASTParser setEnvironment classpath problems [message #1753932 is a reply to message #1064113] Mon, 13 February 2017 05:52 Go to previous message
antonio sarco is currently offline antonio sarcoFriend
Messages: 2
Registered: February 2017
Location: uae
Junior Member
How to use a wildcard in the classpath to add multiple jars

java -cp "lib/*" -jar %MAINJAR%

where %MAINJAR% is the jar file to launch via its internal manifest.

If you need only specific jars, you will need to add them individually. The classpath string does not accept generic wildcards like Jar*, *.jar, hiber* etc.

Example

The following entry does not work:

java -cp "Halo.jar;lib/*.jar" ni.package.MainClass

Correct entry is :

java -cp "Halo.jar;lib/*" ni.package.MainClass



Anto

Previous Topic:Unable to Drag / Move tabs
Next Topic:Avast Security 9.0 blocks debugger port
Goto Forum:
  


Current Time: Sun Apr 14 02:50:31 GMT 2024

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

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

Back to the top