0

I am just wondering, if java has something similar to /usr/lib in C/C++, then we won't have to do

java -cp "lib/*" mypackage.MyClass

Instead we can put all our favorite jar files into some folder like /java/lib/ and do

java mypackage.MyClass

Standardization of such a location also saves you the trouble of having to put the same set of frequently used libraries into many projects repeatedly.

Does such a feature exist already?

1
  • 2
    Seriously... don't do that! It will mess everything up for you eventually. Use maven for dependency management in Java projects. It can package up your applications nicely and I believe you can even get it to build you a monolithic jar file with all dependencies embedded into it. If you don't want that, it will put them in a lib/ directory in your software package and you can use a very simple shell script to add all jar files in that directory to your classpath when you start up your application.
    – StFS
    Nov 7, 2014 at 14:49

2 Answers 2

1

What you are talking about is called the extension folder. Here are summarized some pros and cons about its usage.

0

Possible other ways ways:

Use the CLASSPATH environment variable. If -cp or -classpath parameters are not specified, the value of the CLASSPATH environment variable will be used as classpath (if it exists). More on setting the CLASSPATH: PATH and CLASSPATH

The META-INF/MANIFEST.MF file inside jars can specify the class path. Add the following line to specify required libraries:

Class-Path: lib/myjar.jar lib/someotherjar.jar

More on this: Adding Classes to the JAR File's Classpath

2
  • 1
    problem with this is that you can't point the CLASSPATH environment variable to a directory and tell it to "add all jars in that directory". It will only add the directory itself meaning that Java expects class files to be located there. You would need to do this with a script by looping through this directory and adding all jar files there to the CLASSPATH variable. But this would mean that if you add new jar files they won't be picked up until after you restart your session.
    – StFS
    Nov 7, 2014 at 14:52
  • @StFS Yes, I'm aware of that. You have to list the jars or class folders.
    – icza
    Nov 7, 2014 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.