why some imports cannot be resolved?

2019-09-20 17:06发布

问题:

I'm new to java , i tried to use word net for senitment analysis . when i used class of wordnet , i got the following failure in importing

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.lucene.analysis.en.EnglishMinimalStemmer;
import org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileListener;
import org.apache.stanbol.commons.stanboltools.datafileprovider.DataFileTracker;
import org.apache.stanbol.enhancer.engines.sentiment.api.LexicalCategoryClassifier;
import org.apache.stanbol.enhancer.engines.sentiment.api.SentimentClassifier;
import org.apache.stanbol.enhancer.engines.sentiment.util.WordSentimentDictionary;
import org.apache.stanbol.enhancer.nlp.pos.LexicalCategory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;

all the above imports cannot be resolved like org.apache.felix , org.apache.lucene ,org.apache.stanbol,org.osgi

回答1:

This is because your compiler is not able to find the necessary packages and or libraries that are needed to resolve these imports. These packages must be included in your class path. For example all of the errors regarding

org.apache.felix.scr.annotations.x

can be resolved after downloading the latest .jar from https://mvnrepository.com/artifact/org.apache.felix/org.apache.felix.scr.annotations/1.11.0

Follow these steps to include jar files in your class path. -

  • Drag the required jar file from your download directory to the src directory of your project in eclipse
  • Right click on the jar file, Select Build Path and then select Add To Build Path option.
  • A dialogue box will appear asking you to link all files in the jar file, just stick with the defaults and hit OK.
  • You are done now, all your errors regarding imports will be resolved.


回答2:

These packages need to be within the compiler's class path.

Another way to say it : the compiler needs to be able to know where to find these files. This imposes several constraints:

  • these files need to actually exist in your hard drive (whether added manually, or automatically by a dependency manager)
  • they should be organized in folders that match the parts of the package name
  • the folder where they are must be within the class path, which can be specified with the 'classpath' compiler option, or in your IDE's options