-2

I have this folder structure:

|___main
| |___lib -> this folder contains my runnable jar file
| |___ext -> this folder contains dependency files (.jar) for the jar
|___log4j.properties -> here I have log4j.property file

How can I execute the JAR without ClassNotFoundException or any other error? I am using Eclipse Luna and this is a Java Project.

Zenadix
  • 15,291
  • 4
  • 26
  • 41
  • There is not enough information here to answer. I can only recommend you read about [the classpath](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html). – Boris the Spider Oct 02 '14 at 15:05
  • above mentioned is the folder structure, if i run java -cp is permanent it won't change. – Shiva Shiva Oct 02 '14 at 15:09
  • You can include all the dependencies *inside* the jar file, and you will never get any `ClassNotFoundException`. – Zenadix Oct 02 '14 at 15:11

1 Answers1

0

You can include all your dependencies inside your JAR file. Please see this answer on how to do it.

There are also tools that automate this process, such as Ant and Maven.

Community
  • 1
  • 1
Zenadix
  • 15,291
  • 4
  • 26
  • 41
  • what about log4j.properties? – Shiva Shiva Oct 02 '14 at 15:34
  • @ShivaShiva You can definitely put your log4j.properties file into your jar. Log4j loads its configuration by searching the class path, so if you put in into the root of your jar, it should be found. – Konstantin Tarashchanskiy Oct 02 '14 at 15:37
  • Add `log4j.properties` to your JAR file. – Zenadix Oct 02 '14 at 15:38
  • i want the log4j to be edited, i can't extract jar to edit log4j properties – Shiva Shiva Oct 02 '14 at 15:38
  • @ShivaShiva Then supply the path to it with `-Dlog4j.configuration=file:/path/to/log4j.properties`, see [this answer](http://stackoverflow.com/a/7390591/2071828). – Boris the Spider Oct 02 '14 at 16:00
  • 1
    @Zenadix the answer you linked does this manually, surely you are not advocating that; its an absolutely terrible idea. Either explain how you would do this using a build tool, such as Maven or Ant, or turn this "answer" into a comment. – Boris the Spider Oct 02 '14 at 16:03
  • @BoristheSpider I strongly believe new Java programmers should learn to do this manually before learning to use build tools such as Maven or Ant. Doing so allows them to *understand* how Java really works. – Zenadix Oct 02 '14 at 16:21
  • While that's true, if you don't explain how to do it "right" almost immediately, they'll just persist in using a slow, inconsistent, laborious, accident-prone method forever, or worse, roll their own ad hoc build process. Understanding how it works is good. Doing it right is better. – Ian McLaird Oct 02 '14 at 17:51