|
Joe, Actually writing your own code to add classes/jars from specified directories is not that bad, Ive done similar myself. A lot of open source java projects require a lot of jar files added to the classpath which is a pain, so I created a custom classloader so the I could read a list of directories from a properties file, and load all the jars in those directories public class Runner extends URLClassLoader{ ... public static void main(String[] args) throws Throwable { if (args.length >= 1) { // get paths to be used for loading ClassLoader base = ClassLoader.getSystemClassLoader(); URL[] urls; if (base instanceof URLClassLoader) { urls = ((URLClassLoader)base).getURLs(); } else { urls = new URL[] { new File(".").toURI().toURL() }; } // list the paths actually being used System.out.println("Loading from paths:"); for (int i = 0; i < urls.length; i++) { System.out.println(" " + urls[i]); } // load target class using custom class loader Runner loader = new Runner(urls, base.getParent()); loader.log(args[0]); loader.setEnv(args[0]); Class clas = loader.loadClass(loader.getClass(args[0])); // invoke "main" method of target class Class[] ptypes = new Class[] { args.getClass() }; Method main = clas.getDeclaredMethod("main", ptypes); String[] pargs = new String[args.length-1]; // System.arraycopy(args, 1, pargs, 0, pargs.length); Thread.currentThread().setContextClassLoader(loader); main.invoke(null, new Object[] { args }); } } public void setEnv(String dir, String propertiesFile) throws Throwable { if (env == null) { env = dir; addLib(dir); } // Load the classpath from the specified properties file addJarLib(getClasspath(propertiesFile)); } public void addJarLib(String liblist) throws Throwable { // Create filter if necessary if (jf == null) jf = new JarFilter(); String lib = null; StringTokenizer st = new StringTokenizer(liblist,":"); while (st.hasMoreTokens()) { lib = st.nextToken(); // Get a list of jars in the directory File dir = new File(lib); // Exit if directory not found if (!dir.exists()) { throw new Exception(dir.getAbsolutePath() + " not found in addJarLib"); } File[] files = dir.listFiles(jf); if (files.length == 0) { addURL(dir.toURL()); } else { for (int i = 0; i < files.length; i++) { addURL(files[i].toURL()); } } } } public void addLib(String lib) throws Throwable { // Refer to directory File dir = new File(lib); // Exit if directory not found if (!dir.exists()) { throw new Exception(dir.getAbsolutePath() + " not found in addLib"); } addURL(dir.toURL()); } cheers Colin.W http://as400blog.blogspot.com Extension 5800 Direct dial 0870 429 5800 -----Original Message----- From: Joe Sam Shirah [mailto:jshirah@xxxxxxxxxxxxx] Sent: 09 March 2005 05:41 To: Java Programming on and around the iSeries / AS400 Subject: Re: CLASSPATH Hi Jon, Short of writing your own class loader, there is no way to have all jars in a directory searched via a simple directory entry in the classpath. As David mentions, you have to explicitly list jars on the classpath for the standard case. An alternative is to use the extension mechanism. You can just dump the jars in the ext directory. One issue is that IBM ( and lately Sun ) also use this directory. Also, depending on what you use, there may be issues with new JDK releases. You can use the system property "java.ext.dirs" to denote your own extension directory. The issue here is that the value *replaces* the standard java.ext.dirs, so if you just use your own, classes in the standard one will not be found. Therefore, you would always want to concatenate the standard ext dirs along with your own. Also, you can effectively only set java.ext.dirs with an arg to the java command; setting the property inside an application doesn't work to change the VM's notion of the ext dirs. Personally, I'd like to see a property like "user.ext.dirs" or allow java.ext.dirs to be changed ( and noticed by the VM ) internally in an app with a replace or add option. For V5R3 ( we have JDK 1.3 and 1.4 installed ), the java.ext.dirs property returns: /QIBM/ProdData/OS400/Java400/jdk/lib/ext: /QIBM/UserData/Java400/ext: /QIBM/ProdData/Java400/jdk14/lib/ext: There is also an /QIBM/ProdData/OS400/Java400/ext: directory, but since it is not part of the java.ext.dirs property I don't know if it is used or not. Notice this structure is different from the Sun standard, which is jre/lib/ext. HTH or at least clarifies things. Joe Sam Joe Sam Shirah - http://www.conceptgo.com conceptGO - Consulting/Development/Outsourcing Java Filter Forum: http://www.ibm.com/developerworks/java/ Just the JDBC FAQs: http://www.jguru.com/faq/JDBC Going International? http://www.jguru.com/faq/I18N Que Java400? http://www.jguru.com/faq/Java400 ----- Original Message ----- From: "David Gibbs" <david@xxxxxxxxxxxx> To: "Java Programming on and around the iSeries / AS400" <java400-l@xxxxxxxxxxxx> Sent: Tuesday, March 08, 2005 10:22 PM Subject: Re: CLASSPATH > Jon Paris wrote: > > Can someone point me to the docs that define what can be specified > > in the > > CLASSPATH. I know I can specify multiple jar files by separating entries by > > a colon. But how do I set it up so that (for example) all jars in a > > specified directory will be searched. I can't find this info in the > > IBM docs. > > I don't think there's a way to specify that the classpath will consist > of all files in a specific directory. > > I've always had to write a script that cycles through all the > candidate files and adds them, one by one, to the classpath. > > david > -- -- This is the Java Programming on and around the iSeries / AS400 (JAVA400-L) mailing list To post a message email: JAVA400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options, visit: http://lists.midrange.com/mailman/listinfo/java400-l or email: JAVA400-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at http://archive.midrange.com/java400-l. This e-mail has been sent by a company of Bertram Group Ltd, whose registered office is 1 Broadland Business Park, Norwich, NR7 0WF. This message, and any attachments, are intended solely for the addressee and may contain privileged or confidential information. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. If you believe that you have received this email in error, please contact the sender immediately. Opinions, conclusions and statements of intent in this e-mail are those of the sender and will not bind a Bertram Group Ltd company unless confirmed in writing by a director independently of this message. Although we have taken steps to ensure that this email and any attachments are free from any virus, we advise that in keeping with good computing practice the recipient should ensure they are actually virus free.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.