Reputation: 1535
i try to execute this simple short scripts to test nashorn :
basedir="/cassandara2/cassandra"
ba="/cassandara2/cassandra/lib/ST4-4.0.8.jar";
lib = "";
lib += ' <root url="jar://' + ba.replace(basedir, "$PROJECT_DIR");
print(lib);
it works perfectly but when i add sign $ after "$PROJECT_DIR" ==> "$PROJECT_DIR$" , i got this error:
> java.lang.StringIndexOutOfBoundsException: String index out of range:
> 13 at java.lang.String.charAt(String.java:646) at
> jdk.nashorn.internal.objects.NativeRegExp.appendReplacement(NativeRegExp.java:738)
> at
> jdk.nashorn.internal.objects.NativeRegExp.replace(NativeRegExp.java:674)
> at
>
> jdk.nashorn.internal.objects.NativeString.replace(NativeString.java:763)
> bla...
Is it a bug of jdk 8 nashorn ( i used jdk8 u45) by this code :
public static void main(String[] args) {
try {
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("nashorn");
Object eval = engine.eval("load(\"" + "script/demo.js" + "\");");
System.out.println(eval);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 4
Views: 996
Reputation: 906
This is a bug in the original JDK 1.8.0 release. It was fixed in the 8u20 update release.
It's strange that you report seeing this bug with Java 8u45, because that version definitely contains the fix:
jdk1.8.0_45/bin/jjs
jjs> "string".replace("i", "$");
str$ng
Could it be you have an older JDK 8 release on your path?
Upvotes: 1
Reputation: 1177
Bug filed. https://bugs.openjdk.java.net/browse/JDK-8081608 $ at the end of replace string causes index out of range error
js> "string".replace("i", "$");
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
jjs> "string".replace("i", "$ ");
str$ ng
Upvotes: 1
Reputation: 142
it is not an answer, I have not enough points, but you have regexp exception and $
character is regexp special character. Try to escape it, e.g. '\\\$'
.
Upvotes: 2
Reputation: 358
I just tested this with 2 different JVM versions (jdk SE 1.8.0_60-ea-b16 and jdk SE 1.8.0_45-b14) and it worked perfectly outputting both $ signs before and after PROJECT_DIR with no exceptions.
Can you give us some more details such as what JDK implementation, OS, 32/64 bit JDK
There must be some underlying problem you are having but as a fix you can try the following
replace this JS line:
lib += ' <root url="jar://' + ba.replace(basedir, "$PROJECT_DIR$");
with either of these escape sequences
lib += ' <root url="jar://' + ba.replace(basedir, "\$PROJECT_DIR\$");
//or
lib += ' <root url="jar://' + ba.replace(basedir, "$$PROJECT_DIR$$");
Upvotes: 1