Running Java Program in Sub-Directory

断了今生、忘了曾经 提交于 2019-12-11 05:06:25

问题


Please give suggestions if my formatting is wrong, this is my first time posting a question here. Using UNIX, I'm having trouble running a Java program 'Master' (located in a sub-directory) from the top-level directory. Compiling works just fine from the top-level, but I can't actually run the java code from the top-level. I think I may be misunderstanding classpath / the classpath flag. My directory structure is like so: /top-level/src/amsPassageretriever.

When I try to run everything from the top-level directory, I get the following error:

user:~/top-level$ javac -classpath :/NLP_TOOLS/info_retrieval/lucene/latest/lucene-core-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/demo/lucene-demo-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/wordnet/lucene-wordnet-3.1.0.jar:/NLP_TOOLS/info_retrieval/lemur/latest/share/indri/indri.jar src/amsPassageretriever/*.java
user:~/top-level$ java -classpath :/NLP_TOOLS/info_retrieval/lucene/latest/lucene-core-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/demo/lucene-demo-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/wordnet/lucene-wordnet-3.1.0.jar:/NLP_TOOLS/info_retrieval/lemur/latest/share/indri/indri.jar src/amsPassageretriever/Master
Exception in thread "main" java.lang.NoClassDefFoundError: src/amsPassageretriever/Master (wrong name: amsPassageretriever/Master)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: src/amsPassageretriever/Master.  Program will exit.

回答1:


The argument to java is not a file, it is the fully qualified classname of a class inside the classpath (the main class that you want to execute).

It sounds like like class you want is amsPassageretriever.Master (I am assuming you made a typo calling the package amsPassageretrieve at the end of your post).

Hence you should invoke:

java -classpath :/NLP_TOOLS/info_retrieval/lucene/latest/lucene-core-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/demo/lucene-demo-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/wordnet/lucene-wordnet-3.1.0.jar:/NLP_TOOLS/info_retrieval/lemur/latest/share/indri/indri.jar amsPassageretriever.Master

Update: If you want it to run properly when you are not in src then you need to add that directory to your classpath too:

java -classpath /home/user/top-level/src:/NLP_TOOLS/info_retrieval/lucene/latest/lucene-core-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/demo/lucene-demo-3.1.0.jar:/NLP_TOOLS/info_retrieval/lucene/latest/contrib/wordnet/lucene-wordnet-3.1.0.jar:/NLP_TOOLS/info_retrieval/lemur/latest/share/indri/indri.jar amsPassageretriever.Master



回答2:


try java amsPassageretrieve.Master as amsPassageretrieve is the package as far as i can see

you may also need to add classpath variables if your class requires




回答3:


You are typing "retrieve" and "retriever" which are two different words



来源:https://stackoverflow.com/questions/5943879/running-java-program-in-sub-directory

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