【发布时间】:2017-05-18 17:44:12
【问题描述】:
我使用 apache opennlp 创建了 java 项目,用于 IntelliJ 中的命名实体识别。我在里面使用 jar 文件。
代码在 intelliJ 中运行良好。当我尝试在我的虚拟机(Ubuntu)中运行代码时,代码编译得很好,但是当我尝试运行它时,它给出了以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: opennlp/too /tokenize/TokenizerModel
Caused by: java.lang.ClassNotFoundException:
opennlp.tools.tokenize.TokenizerModel at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
这是我的代码:
import java.io.FileInputStream;
import java.io.InputStream;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.Span;
public class openNLP{
public static void main(String args[]) throws Exception{
//Loading the tokenizer model
InputStream inputStreamTokenizer = new
FileInputStream("/home/user/finalproject/apachenlp/opennlpmodels/en-token.bin");
TokenizerModel tokenModel = new TokenizerModel(inputStreamTokenizer);
//Instantiating the TokenizerME class
TokenizerME tokenizer = new TokenizerME(tokenModel);
//Tokenizing the sentence in to a string array
String sentence = "Neil A. Armstrong, the first man to walk on the moon, was born in Wapakoneta, Ohio,\n" +
"on August 5, 1930. He began his NASA career in Ohio.\n" +
"\n" +
"After serving as a naval aviator from 1949 to 1952, Armstrong joined the National Advisory Committee for Aeronautics (NACA) in 1955. His first assignment was with the NACA Lewis Research Center (now NASA Glenn) in Cleveland. Over the next 17 years, he was an engineer, test pilot, astronaut and administrator for NACA and its successor agency, the National Aeronautics and Space Administration (NASA).\n" +
"\n" +
"As a research pilot at NASA's Flight Research Center, Edwards, Calif., he was a project pilot on many pioneering high speed aircraft, including the well known, 4000-mph X-15. He has flown over 200 different models of aircraft, including jets, rockets, helicopters and gliders.\n" +
"\n" +
"Armstrong transferred to astronaut status in 1962. He was assigned as command pilot for the Gemini 8 mission. Gemini 8 was launched on March 16, 1966, and Armstrong performed the first successful docking of two vehicles in space.\n" +
"\n" +
"As spacecraft commander for Apollo 11, the first manned lunar landing mission, Armstrong gained the distinction of being the first man to land a craft on the moon and first to step on its surface.\n" +
"\n" +
"Armstrong subsequently held the position of Deputy Associate Administrator for Aeronautics, NASA Headquarters, Washington, D.C. In this position, he was responsible for the coordination and management of overall NASA research and technology work related to aeronautics.\n" +
"\n" +
"He was Professor of Aerospace Engineering at the University of Cincinnati between 1971-1979. During the years 1982-1992, Armstrong was chairman of Computing Technologies for Aviation, Inc., Charlottesville, Va.\n" +
"\n" +
"He received a Bachelor of Science Degree in Aeronautical Engineering from Purdue University and a Master of Science in Aerospace Engineering from the University of Southern California. He holds honorary doctorates from a number of universities.\n" +
"\n" +
"Armstrong was a Fellow of the Society of Experimental Test Pilots and the Royal Aeronautical Society; Honorary Fellow of the American Institute of Aeronautics and Astronautics, and the International Astronautics Federation.\n" +
"\n" +
"He was a member of the National Academy of Engineering and the Academy of the Kingdom of Morocco. He served as a member of the National Commission on Space (1985-1986), as Vice-Chairman of the Presidential Commission on the Space Shuttle Challenger Accident (1986), and as Chairman of the Presidential Advisory Committee for the Peace Corps (1971-1973).\n" +
"\n" +
"Armstrong was decorated by 17 countries. He was the recipient of many special honors, including the Presidential Medal of Freedom; the Congressional Gold Medal; the Congressional Space Medal of Honor; the Explorers Club Medal; the Robert H. Goddard Memorial Trophy; the NASA Distinguished Service Medal; the Harmon International Aviation Trophy; the Royal Geographic Society's Gold Medal; the Federation Aeronautique Internationale's Gold Space Medal; the American Astronautical Society Flight Achievement Award; the Robert J. Collier Trophy; the AIAA Astronautics Award; the Octave Chanute Award; and the John J. Montgomery Award.\n" +
"\n";
String tokens[] = tokenizer.tokenize(sentence);
//Loading the NER-person model
InputStream inputStreamNameFinder = new
FileInputStream("/home/user/finalproject/apachenlp/opennlpmodels/en-ner-location.bin");
TokenNameFinderModel model = new TokenNameFinderModel(inputStreamNameFinder);
//Instantiating the NameFinderME class
NameFinderME nameFinder = new NameFinderME(model);
//Finding the names in the sentence
Span nameSpans[] = nameFinder.find(tokens);
//Printing the names and their spans in a sentence
for(Span s: nameSpans)
System.out.println(s.toString()+" "+tokens[s.getStart()]);
}
}
任何帮助将不胜感激。 谢谢
【问题讨论】:
-
“当我尝试在我的虚拟机 (ubuntu) 中运行代码时” 你是如何在那里运行它的?您是否创建了可执行的 jar?
-
依赖项未包含在您的构建中。我强烈建议迁移到 Maven 或 Gradle 来进行构建和依赖管理。
-
我在 copile 命令中给出了 jar 文件的类路径。它编译得很好,但没有运行。
标签: java apache exception noclassdeffounderror opennlp