2

to launch tests , I have to set a big list of jar files as arguments for classpath : -classpath a.var;b.jar;.... - Is there an other way to specify libraries ? for example is it possible to set file as arguments and the file contains path to all libraries example : -classpath myFile.txt and myFile.txt contains ../a.jar ../b.jar .. etc

thanks,

0

5 Answers 5

3

Since Java 6, you can use wildcards in your classpath:

java -classpath 'lib/*'

Note that you must quote the classpath string to avoid having the shell expand the wildcard.

7
  • I think it's rather java -classpath lib/*
    – JB Nizet
    May 5, 2011 at 13:20
  • @Michael Borgwardt: from download.oracle.com/javase/6/docs/technotes/tools/windows/… : "For example, the class path entry foo/* specifies all JAR files in the directory named foo."
    – JB Nizet
    May 5, 2011 at 13:23
  • @JB: that's when foo/* is what gets passed to the java tool. But that will not happen because the shell will expand the wildcard and you effectively call java -classpath foo/a.jar foo/b.jar foo/c.jar - which leads to an error May 5, 2011 at 13:30
  • @Michael Borgwardt: OK. I forgot the quote, and you had lib/*.jar instead of lib/*.
    – JB Nizet
    May 5, 2011 at 13:36
  • @JB: Ah, now I understand what you mean. Will change that. May 5, 2011 at 13:39
3

You can programmatically add a new path to classpath:

String currentPath = System.getProperty("java.library.path");
System.setProperty( "java.library.path", current + ":/path/to/my/libs" );

// this forces JVM to reload "java.library.path" property
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );

You can add a path, so no need to list all jars. This changes classpath only for your JVM instance, so it will not affect other java applications.

Update:

: and / are UNIX-specific lib-path and folder-path separators. For multi-OS you should use "path.separator" and "file.separator" system properties.

2
  • could you develop more please ? example I have lib1.jar,lib2.jar,...libi.jar that are available in different folders how do I can explore your suggestion ? thanks
    – user739991
    May 5, 2011 at 14:22
  • Add more paths: `":/path/to/my/libs1:/path/to/mylibs2:/path/etc" May 5, 2011 at 15:54
2

You can set the environment variable CLASSPATH (use the proper syntax for your shell to do this, ex. bash, Windows XP, etc).

You can also create some sort of profile file that does this for you all the time, ex .bashrc. This will affect every time the java command is used under that profile, of course.

If your main class is in a jar, you can also use the jar's manifest to set the classpath. Sun/Oracle has a tutorial page on doing this. Create a file called Manifest.txt. In that file add the line:

Class-Path: jar1-name jar2-name directory-name/jar3-name

where the various jar1-name parts are actual jars on your classpath.

Then create your jar using:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

Or the Ant Jar Task with manifest attribute set, or use the Maven jar plugin or however your build works, get the manifest set for the jar.

Or continue to use --classpath as you are currently.

2

Since Java 6, you may use wildcards in classpath, to include all jars in a directory. See http://download.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html.

1
  • This is very convenient but very dangerous too, as it relies on the file system for the sort order of the jars. This can potentially cause nasty class path errors in the presence of conflicting classes, as different jars might take precedence in different environments. Nov 7, 2014 at 17:24
1

To lunch tests I strongly suggest you to use Ant.

And Ant has <classpath> element, which allows you to specify "all jars within given directory":

<classpath>
  <pathelement path="${classpath}"/>
  <fileset dir="lib">
    <include name="**/*.jar"/>
  </fileset>
  <pathelement location="classes"/>
  <dirset dir="${build.dir}">
    <include name="apps/**/classes"/>
    <exclude name="apps/**/*Test*"/>
  </dirset>
  <filelist refid="third-party_jars"/>
</classpath>

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.