Beanshell 不允许我将 jar 添加到“默认”环境中。 JRE 类加载器?
我有一个关于 Beanshell 的问题,我在任何地方都找不到答案。我只能通过以下两种方式之一运行 Beanshell 脚本:
在调用 Beanshell 之前定义类路径,并且 Beanshell 使用 JRE 默认类加载器。
在启动 Beanshell 之前根本没有定义类路径,然后我使用
addClassPath()
和importCommands()
动态构建类路径 在 Beanshell 的类加载器中。这个方法好像没有继承jars 这是默认 JRE 类加载器的一部分。
经过多次实验,我了解到我无法使用预定义的类路径启动脚本,然后能够使用 addClassPath()
添加到类路径。我不知道这是设计好的还是我做错了什么?
你自己很容易看出我的问题是什么。例如,下面是脚本:
::Test.bat (where bsh.jar exists in JRE/lib/ext directory)
@echo off
set JAVA_HOME=C:\JDK1.6.0_27
:: first invoke: this first command works
%JAVA_HOME%\jre\bin\java.exe bsh.Interpreter Test.bsh
:: second invoke: this command fails
%JAVA_HOME%\jre\bin\java.exe -cp ant.jar bsh.Interpreter Test.bsh
第二次调用导致此错误:
Evaluation Error: Sourced file: Test.bsh : Command not
found: helloWorld() : at Line: 5 : in file: Test.bsh : helloWorld ( )
Test.bat 启动此 Beanshell 脚本:
// Test.bsh
System.out.println("Trying to load commands at: " + "bin" );
addClassPath("bin");
importCommands("bin");
helloWorld();
而且,这是我的 helloWorld.bsh 脚本:
// File: helloWorld.bsh
helloWorld() {
System.out.println("Hello World!");
}
I have a question about Beanshell that I can't find an answer to anywhere. I am only able to run Beanshell scripts in 1 of 2 ways:
Where Classpath is defined before invoking Beanshell and Beanshell uses
the JRE default classloader.Where no classpath is defined at all before starting Beanshell and then I use
addClassPath()
andimportCommands()
to dynamically build the classpath
within Beanshell's classloader. This method does not seem to inherit a jars
that were part of the default JRE classloader.
After much experimentation, I have learned that I am unable to start a script with a pre-defined Classpath and then be able to add to the classpath by using addClassPath()
. I don't know if this is as-designed or if I am doing something wrong?
It is very easy to see for yourself what my problem is. For example, here is the script:
::Test.bat (where bsh.jar exists in JRE/lib/ext directory)
@echo off
set JAVA_HOME=C:\JDK1.6.0_27
:: first invoke: this first command works
%JAVA_HOME%\jre\bin\java.exe bsh.Interpreter Test.bsh
:: second invoke: this command fails
%JAVA_HOME%\jre\bin\java.exe -cp ant.jar bsh.Interpreter Test.bsh
The second invoke causes this error:
Evaluation Error: Sourced file: Test.bsh : Command not
found: helloWorld() : at Line: 5 : in file: Test.bsh : helloWorld ( )
Test.bat launches this Beanshell script:
// Test.bsh
System.out.println("Trying to load commands at: " + "bin" );
addClassPath("bin");
importCommands("bin");
helloWorld();
And, this is my helloWorld.bsh script:
// File: helloWorld.bsh
helloWorld() {
System.out.println("Hello World!");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
Test.bsh
有一个小错误:importCommands
在类路径中查找名为“bin”的目录并从那里加载所有 .bsh 文件,因此您应该添加什么toaddClassPath
是当前目录:您的代码在第一种情况下有效,因为当前目录位于默认的系统类路径中。问题在于 -cp 开关覆盖了默认的类路径,因此 importCommands 不再能够找到 bin 目录。
或者,您可以将
.
添加到 JVM 级别的类路径中:Your
Test.bsh
has a slight error:importCommands
looks for a directory called "bin" in the class path and loads all .bsh files from there, so what you should add toaddClassPath
is the current directory:The code you had works in the first case because the current directory is in the default system class path. The problem is that the
-cp
switch overrides the default class path, soimportCommands
no longer has any way to find thebin
directory.Alternatively you can add
.
to the classpath on the JVM level: