Permalink
Browse files

Appease URLClassLoader which requires directories to end in slashes

  • Loading branch information...
nicksieger authored and BanzaiMan committed Dec 8, 2010
1 parent 9cf97c3 commit 8740a6b3ea946a1442dd7fa833aed3c30d82e23f
Showing with 35 additions and 7 deletions.
  1. +21 −0 spec/java_integration/extensions/classpath_spec.rb
  2. +14 −7 src/org/jruby/RubyClassPathVariable.java
@@ -0,0 +1,21 @@
+require File.dirname(__FILE__) + "/../spec_helper"
+
+require 'java'
+
+describe "The $CLASSPATH variable" do
+ it "appends URLs unmodified" do
+ $CLASSPATH << "http://jruby.org/"
+ $CLASSPATH.should include("http://jruby.org/")
+ end
+
+ it "assumes entries without URL protocols are files" do
+ $CLASSPATH << __FILE__
+ $CLASSPATH.should include("file:#{__FILE__}")
+ end
+
+ it "appends slashes to directory names" do
+ d = File.expand_path(File.dirname(__FILE__))
+ $CLASSPATH << d
+ $CLASSPATH.should include("file:#{d}/")
+ end
+end
@@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (C) 2007 Ola Bini <ola@ologix.com>
- *
+ *
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
@@ -44,7 +44,6 @@ public static void createClassPathVariable(Ruby runtime) {
RubyClassPathVariable self = new RubyClassPathVariable(runtime);
runtime.getEnumerable().extend_object(self);
runtime.defineReadonlyVariable("$CLASSPATH", self);
-
self.getMetaClass().defineAnnotatedMethods(RubyClassPathVariable.class);
}
@@ -65,10 +64,18 @@ public IRubyObject append(IRubyObject obj) {
}
private URL getURL(String target) throws MalformedURLException {
- if (target.indexOf("://") == -1) {
- return new URL("file", null, target);
- } else {
+ try {
+ // First try assuming a protocol is included
return new URL(target);
+ } catch (MalformedURLException e) {
+ // Assume file: protocol
+ File f = new File(target);
+ String path = target;
+ if (f.exists() && f.isDirectory() && !path.endsWith("/")) {
+ // URLClassLoader requires that directores end with slashes
+ path = path + "/";
+ }
+ return new URL("file", null, path);
}
}
@@ -90,10 +97,10 @@ public IRubyObject each(Block block) {
@JRubyMethod(name = "to_s")
public IRubyObject to_s() {
return callMethod(getRuntime().getCurrentContext(), "to_a").callMethod(getRuntime().getCurrentContext(), "to_s");
- }
+ }
@JRubyMethod(name = "inspect")
public IRubyObject inspect() {
return callMethod(getRuntime().getCurrentContext(), "to_a").callMethod(getRuntime().getCurrentContext(), "inspect");
- }
+ }
}// RubyClassPathVariable

0 comments on commit 8740a6b

Please sign in to comment.