【问题标题】:How to add external lib to beanshell如何将外部库添加到 beanshell
【发布时间】:2022-06-19 22:40:24
【问题描述】:

我有一个使用这个库的 beanshell 代码:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.Analytics.Data.Ga.Get;
import com.google.api.services.analytics.model.Accounts;
import com.google.api.services.analytics.model.GaData;
import com.google.api.services.analytics.model.GaData.DataTable;
import com.google.api.services.analytics.model.GaData.DataTable.Rows;
import com.google.api.services.analytics.model.GaData.DataTable.Rows.C;
import com.google.api.services.analytics.model.Profiles;
import com.google.api.services.analytics.model.Webproperties;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpRequest;

...

Line 28 : JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

我下载了 google-api-java-client jars,并在我的代码开头添加了这个:

addClassPath( "/Users/work/google-api-java-client/libs/" );

但它似乎不起作用,运行我的代码时仍然出现此错误:

Evaluation Error: Sourced file: call_google_analytics.txt :
Typed variable declaration : Class: JsonFactory not found in namespace : 
at Line: 28 : in file: call_google_analytics.txt : JsonFactory 

我做错了什么?

【问题讨论】:

    标签: java google-api beanshell


    【解决方案1】:

    当涉及到将项目添加到Interpreter 的类路径时映射目录中的所有 jar 时,我认为这是 Beanshell 中的一个错误。

    我已经在您可以依赖的最新版本上运行了以下测试(没有从 sources 构建 jar)。 那将是来自2016org.apache-extras.beanshell:bsh:2.0b6

    我尝试了manual 提供的几种修改类路径的选项,但似乎都没有效果:

    • 导入 *;
    • reloadClasses();
    • reloadClasses("com.google.*");

    您可以通过查看以下脚本的控制台 (System.err) 输出来验证您的文件夹是否已正常添加,但只是未映射:

    addClassPath("/test-project/lib/");
    reloadClasses("com.google.*");
    

    控制台输出:

    Start ClassPath Mapping
    Mapping: Directory C:\~\test-project\lib
    End ClassPath Mapping
    

    这表明添加的目录是已知的,但没有映射目录中的 jar 文件。否则,它们将被打印在此输出中。

    我已将此追溯到bsh.classpath.BshClassPath 中的以下代码:

        synchronized void map( URL url ) 
            throws IOException 
        { 
            String name = url.getFile();
            File f = new File( name );
    
            if ( f.isDirectory() ) {
                classMapping( "Directory "+ f.toString() );
                map( traverseDirForClasses( f ), new DirClassSource(f) );
            } else if ( isArchiveFileName( name ) ) {
                classMapping("Archive: "+url );
                map( searchJarForClasses( url ), new JarClassSource(url) );
            } 
    
    

    我们可以看到,在if ( f.isDirectory() 上,它仅通过traverseDirForClasses( f ) 扫描。如果您添加单个 Jar 文件,然后通过else if ( isArchiveFileName( name ) ,它会扫描文件以查找带有searchJarForClasses( url ) 的类。我不知道为什么它不扫描目录中的 jar 文件然后是类。

    就我而言,这是一个错误,但我尚未使用来自 github 的最新 3.0 源验证这一点。

    解决方法是单独添加每个jar,见以下脚本+输出:

    libPath = "/test-project/lib/";
    addClassPath(libPath+"google-api-client-1.33.0.jar");
    addClassPath(libPath+"google-http-client-1.41.0.jar");
    addClassPath(libPath+"google-http-client-apache-v2-1.41.0.jar");
    addClassPath(libPath+"google-http-client-gson-1.41.0.jar");
    
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.gson.GsonFactory;
    
    JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
    return "Factory: " + JSON_FACTORY;
    

    输出:

    Factory: com.google.api.client.json.gson.GsonFactory@1a407d53
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 2022-11-12
      • 2021-09-29
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多