Can a directory be added to the class path at runtime?

You can use the following method: URLClassLoader.addURL(URL url) But you’ll need to do this with reflection since the method is protected: public static void addPath(String s) throws Exception { File f = new File(s); URL u = f.toURL(); URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class urlClass = URLClassLoader.class; Method method = urlClass.getDeclaredMethod(“addURL”, new Class[]{URL.class}); method.setAccessible(true); method.invoke(urlClassLoader, … Read more

Set folder for classpath [duplicate]

If you are using Java 6 or higher you can use wildcards of this form: java -classpath “.;c:\mylibs\*;c:\extlibs\*” MyApp If you would like to add all subdirectories: lib\a\, lib\b\, lib\c\, there is no mechanism for this in except: java -classpath “.;c:\lib\a\*;c:\lib\b\*;c:\lib\c\*” MyApp There is nothing like lib\*\* or lib\** wildcard for the kind of job … Read more

How do I auto load a database jar in Groovy without using the -cp switch?

Summarized from Groovy Recipes, by Scott Davis, Automatically Including JARs in the ./groovy/lib Directory: Create .groovy/lib in your login directory Uncomment the following line in ${GROOVY_HOME}/conf/groovy-starter.conf load !{user.home}/.groovy/lib/*.jar Copy the jars you want included to .groovy/lib It appears that for Groovy 1.5 or later you get this by default (no need to edit the conf), … Read more

Getting Ant to recognise a classpath

Here’s an example from a project I am currently working on. I suspect you can modify it to work for your situation. <path id=”master-classpath”> <fileset dir=”${web.dir}/WEB-INF/lib”> <include name=”*.jar”/> </fileset> <fileset dir=”${appserver.lib}”> <include name=”servlet*.jar”/> </fileset> <pathelement path=”${build.dir}”/> </path> … <javac destdir=”${build.dir}”> <src path=”${src.dir}”/> <classpath refid=”master-classpath”/> </javac>

Using bash, how do you make a classpath out of all files in a directory?

[*] New Answer (October 2012) There’s no need to manually build the classpath list. Java supports a convenient wildcard syntax for directories containing jar files. java -cp “$LIB/*” (Notice that the * is inside the quotes.) Explanation from man java: As a special convenience, a class path element containing a basename of * is considered … Read more

I am getting “java.lang.ClassNotFoundException: com.google.gson.Gson” error even though it is defined in my classpath

In case of a JSP/Servlet webapplication, you just need to drop 3rd party JAR files in /WEB-INF/lib folder. If the project is a Dynamic Web Project, then Eclipse will automatically take care about setting the buildpath right as well. You do not need to fiddle with Eclipse buildpath. Don’t forget to undo it all.

Can I create a custom classpath on a per application basis in Tomcat

From Tomcat 7 there is no mention of not being able to use the VirtualWebappLoader in production. I tried it and it works like a dream. Simply add the following to META-INF/context.xml: <?xml version=”1.0″ encoding=”UTF-8″?> <Context antiJARLocking=”true” path=”/websandbox”> <Loader className=”org.apache.catalina.loader.VirtualWebappLoader” virtualClasspath=”/usr/…/*.jar;/usr/…/*.jar”/> </Context> In Netbeans, under packaging, I just untick all the packages, taking the .war … Read more