KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > martiansoftware > nailgun > builtins > NGClasspath


1 /*
2
3   Copyright 2004, Martian Software, Inc.
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16
17 */

18
19 package com.martiansoftware.nailgun.builtins;
20
21 import java.io.File JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLClassLoader JavaDoc;
24
25 import com.martiansoftware.nailgun.NGContext;
26
27 /**
28  * <p>Provides a means to display and add to the system classpath at runtime.
29  * If called with no arguments, the classpath is displayed. Otherwise, each
30  * argument is turned into a java.io.File and added to the classpath. Relative
31  * paths will be resolved relative to the directory in which the nailgun server
32  * is running. This is very likely to change in the future.</p>
33  *
34  * <p>This is aliased by default to the command "<code>ng-cp</code>".</p>
35  *
36  * @author <a HREF="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
37  */

38 public class NGClasspath {
39     
40     /**
41      * Adds the specified URL (for a jar or a directory) to the System
42      * ClassLoader. This code was written by antony_miguel and posted on
43      * http://forum.java.sun.com/thread.jsp?forum=32&thread=300557&message=1191210
44      * I assume it has been placed in the public domain.
45      *
46      * @param url the URL of the resource (directory or jar) to add to the
47      * System classpath
48      * @throws Exception if anything goes wrong. The most likely culprit, should
49      * this ever arise, would be that your VM is not using a URLClassLoader as the
50      * System ClassLoader. This would result in a ClassClastException that you
51      * probably can't do much about.
52      */

53     private static void addToSystemClassLoader(URL JavaDoc url) throws Exception JavaDoc {
54         URLClassLoader JavaDoc sysloader = (URLClassLoader JavaDoc) ClassLoader.getSystemClassLoader();
55         Class JavaDoc sysclass = URLClassLoader JavaDoc.class;
56
57         java.lang.reflect.Method JavaDoc method = sysclass.getDeclaredMethod("addURL", new Class JavaDoc[] {URL JavaDoc.class});
58         method.setAccessible(true);
59         method.invoke(sysloader, new Object JavaDoc[]{url});
60     }
61     
62     public static void nailMain(NGContext context) throws Exception JavaDoc {
63         String JavaDoc[] args = context.getArgs();
64         if (args.length == 0) {
65             URLClassLoader JavaDoc sysLoader = (URLClassLoader JavaDoc) ClassLoader.getSystemClassLoader();
66             URL JavaDoc[] urls = sysLoader.getURLs();
67             for (int i = 0; i < urls.length; ++i) {
68                 context.out.println(urls[i]);
69             }
70         } else {
71             for (int i = 0; i < args.length; ++i) {
72                 File JavaDoc file = new File JavaDoc(args[i]);
73                 addToSystemClassLoader(file.toURL());
74             }
75         }
76     }
77 }
78
Popular Tags