Beanshell不允许我将jar添加到“默认"目录中. JRE类加载器? [英] Beanshell will not allow me to add jars to the "default" JRE classloader?

查看:362
本文介绍了Beanshell不允许我将jar添加到“默认"目录中. JRE类加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Beanshell 的问题,我找不到任何地方的答案.我只能以2种方式之一运行Beanshell脚本:

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:

  1. 在调用Beanshell和Beanshell使用之前定义类路径的位置 JRE默认类加载器.

  1. Where Classpath is defined before invoking Beanshell and Beanshell uses the JRE default classloader.

在启动Beanshell之前根本没有定义类路径的地方,然后我使用 addClassPath()importCommands()动态构建类路径 在Beanshell的类加载器中.这个方法似乎没有继承罐子 是默认JRE类加载器的一部分.

Where no classpath is defined at all before starting Beanshell and then I use addClassPath() and importCommands() 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.

经过大量实验,我了解到我无法使用预定义的类路径启动脚本,然后无法使用addClassPath()添加到类路径中.我不知道这是不是设计的或我做错了什么?

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启动此Beanshell脚本:

Test.bat launches this Beanshell script:

// Test.bsh
System.out.println("Trying to load commands at: " + "bin" );
addClassPath("bin");  
importCommands("bin");
helloWorld();

这是我的helloWorld.bsh脚本:

And, this is my helloWorld.bsh script:

// File: helloWorld.bsh
helloWorld() { 
    System.out.println("Hello World!");
}

推荐答案

您的Test.bsh有一个小错误:importCommands在类路径中查找名为"bin"的目录,并从该目录中加载所有.bsh文件,所以您应该添加到addClassPath的是当前目录:

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 to addClassPath is the current directory:

// Test.bsh
System.out.println("Trying to load commands at: " + "bin" );
addClassPath(".");  // current directory
importCommands("bin");
helloWorld();

您的代码在第一种情况下有效,因为当前目录位于默认系统类路径中.问题是-cp开关将覆盖默认的类路径,因此importCommands不再具有找到bin目录的任何方法.

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, so importCommands no longer has any way to find the bin directory.

或者,您可以将.添加到JVM级别的类路径:

Alternatively you can add . to the classpath on the JVM level:

%JAVA_HOME%\jre\bin\java.exe -cp .;ant.jar bsh.Interpreter Test.bsh

这篇关于Beanshell不允许我将jar添加到“默认"目录中. JRE类加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆