5

This is probably a pretty stupid question. I just learned how to use external libraries in java by adding the .jar file to the classpath and whatnot, but where exactly do you keep the .jar file? In a video tutorial about adding .jar files to the classpath, the programmer just kept the .jar file in his downloads folder. So can you put the file anywhere? Or is there a general paradigm as to where you keep your library files that I should abide by?

user3002473
  • 4,835
  • 8
  • 35
  • 61
  • 2
    usually jar files are put into lib folder inside your project – Iłya Bursov Dec 28 '14 at 06:02
  • possible duplicate of [Where to put the external jars?](http://stackoverflow.com/questions/325524/where-to-put-the-external-jars) – Iłya Bursov Dec 28 '14 at 06:03
  • 2
    You may also want to do some research into dependency manager systems, like Maven – MadProgrammer Dec 28 '14 at 06:15
  • Generally speaking, that will come down to your needs. A dependency manager, like Maven will take care of maintaining the correct references to versions of the libraries you are using. If you're doing without a dependency manager, you will need to devise your own means of controlling the libraries, but generally speaking, if you can avoid it, you should also avoid including them within any source control system you might be using... – MadProgrammer Dec 28 '14 at 06:18
  • 1
    The answer depends. Are you building a custom project by yourself, then it only matters that you can find the dependencies in some way which is logical to you. Are you building a project with multiple developers; then you need a means by which other developers can manage the dependencies in away which doesn't break the code for other users but is also easily manageable. Depending if the users are on the same network will provide different solutions, such as automatic dependency management system. The time between updates may also affect your choices. – MadProgrammer Dec 28 '14 at 06:44

3 Answers3

4

It is better to use Dependency Management Systems like maven but if you have restriction for that then,

It's always better to put your jar file inside in your project lib folder because in migration time it will help you to keep all thing at one place.

In case of multiple project it will be better to have one centralize location on server where you put your jar files.

atish shimpi
  • 4,873
  • 2
  • 32
  • 50
  • Does that mean that when you create another project, you create another copy? Seems redundant to me – MadProgrammer Dec 28 '14 at 06:14
  • In case of multiple project it will be better to have one centralize location on server where you put your common jars like `maven`. But if you are running single project I will prefer lib is preferred location for your jar files. – atish shimpi Dec 28 '14 at 06:15
  • So, maybe use a dependency management system like Maven or Ivy ... – MadProgrammer Dec 28 '14 at 06:19
  • Yes but in some projects organization block the many of sites for security reasons, so at that point so can not use maven. and for such projects its better to have architecture what I have specified above. – atish shimpi Dec 28 '14 at 06:21
  • Typically a bad idea to include these resources within the context of the source control system and the location of these resources may be dependent on the build system - IMHO – MadProgrammer Dec 28 '14 at 06:33
2

A .jar file can be kept anywhere as long as the JVM could access it. If you use java or javaw command to execute your java application, specifying the .jar file with a -cp switch is enough for java to know where to find extra classes.

So if you've downloaded commons-io-2.4.jar in your Download folder, you can use the following command to run your program with all classes in the Apache Commons IO library:

java -cp ~/Downloads/commons-io-2.4.jar YourMainClassName

But even though you can refer to a .jar file anywhere, it's a convention to put all external libraries a project needed in a lib/ directory, so they can be easily found.

References:

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html, see the -cp section especially

Haochen Xie
  • 360
  • 2
  • 9
0

In normal use cases to run simple Java applications putting the JAR anywhere will work where run through IDE.

BUT

When you are into Web Application development you will see that it is necessary to put external JARs in Web-INF/lib. Else when you deploy the application, the server will not find the JAR in Runtime and will throw java.lang.ClassNotFoundException. You can also put the JARs directly under Server lib folder but that is not usual convention.

For Swing/SWT applications the external JARs need to be packaged/shipped with the executable application. In those cases you should put the JAR files within your project folder preferably under an LIB folder. For example look at this site: Executable JAR

ZakiMak
  • 2,072
  • 2
  • 17
  • 26