Is there a way to modify module path and added modules of a programmatic JShell instance?

ⅰ亾dé卋堺 提交于 2019-12-07 04:24:46

问题


I'm trying to run some Java code at run-time through a JShell instance I created using the JShell API. To demonstrate my problem, I'm going to share my simple code.

With my current setup I have a directory called lib that has the MySQL Java driver: mysql-connector-java-5.1.35.jar.

Launching JShell via command tool and adding the needed module as:

jshell --module-path lib --add-modules mysql.connector.java

and then loading the mysql driver works for me :

jshell> Class.forName("com.mysql.jdbc.Driver").newInstance();
$1 ==> com.mysql.jdbc.Driver@42f93a98

I've created a similar Java 9 module with module-info.java as:

module example.loadmysql {
    requires java.sql;
    requires mysql.connector.java;
    requires jdk.jshell;
}

src/example/loadmysql/Runner.java as :

package example.loadmysql;

import jdk.jshell.*;
import java.sql.*;

public class Runner {
    public static void main(String[] args) throws Exception {
        // this works because this module requires mysql.connector.java
        System.out.println(Class.forName("com.mysql.jdbc.Driver").newInstance());

        JShell js = JShell.create();
        String code = ""
            + "try {"
            + "    Class.forName(\"com.mysql.jdbc.Driver\").newInstance();"
            + "} catch (Exception e) {"
            + "    System.out.println(e.toString());"
            + "}";
        js.eval(code);
    }
}

After building/packaging:

java -p lib -m example.loadmysql
com.mysql.jdbc.Driver@6a4f787b
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

It's clear that even though the example.loadmysql module requires the mysql connector, the created JShell instance doesn't. So it can't find the class.

Any ideas on how to programmatically add modules to a JShell instance, so it works like the direct JShell coding example?

UPDATE - I've figured out how to set the module path:

String modulePath = System.getProperty("jdk.module.path");
js.eval("System.setProperty(\"jdk.module.path\", \""
    + modulePath + "\");");

But that's not quite enough. I still have add the needed module somehow.


回答1:


You can probably use addToClassPath before eval in your code as:

JShell js = JShell.create();
js.addToClasspath("path/to/add/to/the/classpath");
String code = ""
        + "try {"
        + "    Class.forName(\"com.mysql.jdbc.Driver\").newInstance();"
        + "} catch (Exception e) {"
        + "    System.out.println(e.toString());"
        + "}";
js.eval(code);

The specified path is added to the end of the classpath used in eval(). Note that the unnamed package is not accessible from the package in which eval(String) code is placed.

It seems from the documentation that the state of JShell returned post eval executes the code based on the classpath, hence in order to add any further dependencies to it, you would need to add it to the classpath using the same method.


In your case I am guessing here though as you do so, the mysql-connector-java-5.1.35.jar would ideally be treated to as an automatic module present on the classpath and hence the class com.mysql.jdbc.Driver would be accessible.


Update :- Exploring further I think a better way to achieve this could be though trying to use Jshell.Builder and its option compilerOptions to create an instance with default compiling options somewhat like(not tested) -

JShell js = JShell.builder()
                 .compilerOptions("--module-path lib","--add-modules mysql.connector.java").build();
String code = ""
    + "try {"
    + "    Class.forName(\"com.mysql.jdbc.Driver\").newInstance();"
    + "} catch (Exception e) {"
    + "    System.out.println(e.toString());"
    + "}";
js.eval(code);



回答2:


You can use the /env command to add modules, see help:

/env [-class-path <path>] [-module-path <path>] [-add-modules <modules>] ...
|   view or change the evaluation context

Details:

jshell> /help context
|  
|  context
|  
|  These options configure the evaluation context, they can be specified when
|  jshell is started: on the command-line, or restarted with the commands /env,
|  /reload, or /reset.
|  
|  They are:
|   --class-path <class search path of directories and zip/jar files>
|       A list of directories, JAR archives,
|       and ZIP archives to search for class files.
|       The list is separated with the path separator
|       (a : on unix/linux/mac, and ; on windows).
|   --module-path <module path>...
|       A list of directories, each directory
|       is a directory of modules.
|       The list is separated with the path separator
|       (a : on unix/linux/mac, and ; on windows).
|   --add-modules <modulename>[,<modulename>...]
|       root modules to resolve in addition to the initial module.
|       <modulename> can also be ALL-DEFAULT, ALL-SYSTEM,
|       ALL-MODULE-PATH.
|   --add-exports <module>/<package>=<target-module>(,<target-module>)*
|       updates <module> to export <package> to <target-module>,
|       regardless of module declaration.
|       <target-module> can be ALL-UNNAMED to export to all
|       unnamed modules. In jshell, if the <target-module> is not
|       specified (no =) then ALL-UNNAMED is used.
|  
|  On the command-line these options must have two dashes, e.g.: --module-path
|  On jshell commands they can have one or two dashes, e.g.: -module-path


来源:https://stackoverflow.com/questions/46399783/is-there-a-way-to-modify-module-path-and-added-modules-of-a-programmatic-jshell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!