Reputation: 21
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,
Upvotes: 2
Views: 18272
Reputation: 80340
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.
Upvotes: 3
Reputation: 346327
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.
Upvotes: 3
Reputation: 691805
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.
Upvotes: 2
Reputation: 14766
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.
Upvotes: 2
Reputation: 18336
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>
Upvotes: 1