-1

Note: I have ran the program in IntelliJ without error, I need to make it independent of IDE, i.e. run it under command line. I got the error not because I didn't implement it, but I didn't understand how I can let compiler know it.

I have a project composed of 7 to 10 java files, relies on 5 jar files. The project is managed in IntelliJ project, version 2017.1.

Now I want to run a java class with a main function from the project. In the command line of local computer, I run the class with script:

/project/out/path> java retrieve_result

I correct my script as How to run java program in command prompt,created by intellij .

Note: in the retrieve_result class, there are dependencies on other java and jar files.

The error is shown as below:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/store/Directory
        at HW2MainLucene.ReadIndex(HW2MainLucene.java:69)
        at HW2MainLucene.main(HW2MainLucene.java:23)
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.store.Directory
        at jdk.internal.loader.BuiltinClassLoader.loadClass(java.base@9-internal/BuiltinClassLoader.java:366)
        at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(java.base@9-internal/ClassLoaders.java:184)
        at java.lang.ClassLoader.loadClass(java.base@9-internal/ClassLoader.java:419)
        ... 2 more

As far as I have learned, this error comes because the compiler cannot locate the jar files. But in the IntelliJ project, it is already configured.

How can I configure the compiler to get a usable program?

  • the command line has no concept of what is or is not configured in the IDE –  Jan 29 '18 at 16:15
  • Then do you have any suggestions on adding those knowledge to the command line? It's better if you have some documents or blogs about my purpose, i.e. run the program independent of IDE. – Zhou Junyu Jan 29 '18 at 17:02
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. Because at that point, you will have done whatever research necessary to make it a good question worth asking!"* –  Jan 29 '18 at 17:31
  • I've got your point, thanks for advice. – Zhou Junyu Jan 29 '18 at 17:45

1 Answers1

0

What does "already configured" mean to you?

The error suggests that you have not added those JARs to the CLASSPATH properly in IntelliJ.

I'll assume that you are not using Maven. You've got a /lib folder in your project, at the same level as your /src folder, that contains the JARs your app needs.

Open Project Structure (Crtl+Alt+Shift+S), click on Libraries, and add the /lib directory as a Source Archive Directory. That should put all the JARs it finds inside in your CLASSPATH.

Packaging to run outside the IDE will be another matter. I predict that you will have problems once you try to deploy outside of IntelliJ.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • You are in the right guess. There is the /lib directory just in the same directory, but I add it through `module`, not `library` in project structure. My final purpose is to package them and run in command, i.e. outside the IDE. Do you have any suggestion on it? – Zhou Junyu Jan 29 '18 at 17:01
  • Yes, learn how to create an executable fat JAR file. Maven can do it automatically, but I fear that might be too much to ask from you. https://dzone.com/articles/java-8-how-to-create-executable-fatjar-without-ide – duffymo Jan 29 '18 at 17:31
  • Thanks for your patience. I am grateful for your help. – Zhou Junyu Jan 29 '18 at 17:47
  • Start by running it successfully in IntelliJ. Once it’s running figure out how to do it without the IDE. – duffymo Jan 29 '18 at 17:48
  • Yes, I am now working on it. The project has been completed last year, my current challenge is to run it without the IDE. – Zhou Junyu Jan 29 '18 at 19:00
  • See the link I posted earlier. Or learn Maven and its shade plugin. This is a solved problem. – duffymo Jan 29 '18 at 19:05