简体   繁体   English

使用Shebang运行nashorn不接受-cp选项

[英]running nashorn using Shebang does not accept -cp option

I am trying to use a class into a jar using a Nashorn Shebang script with the -cp option (java version "1.8.0_31"). 我正在尝试使用带有Nashorn Shebang脚本和-cp选项(Java版本“ 1.8.0_31”)的jar类。 However it does not works. 但是,它不起作用。 I have perform some test. 我已经做了一些测试。 The following shebang line works: 以下shebang行有效:

  • #!/usr/bin/jjs -scripting #!/ usr / bin / jjs-脚本
  • #!/usr/bin/jjs -fv (returns nashorn full version 1.8.0_31-b13) #!/ usr / bin / jjs -fv(返回nashorn完整版本1.8.0_31-b13)

while the following not: 而以下不是:

  • #!/usr/bin/jjs -cp ./some/lib/lib.jar will return the following error message: "-cp ./some/lib/lib.jar" is not a recognized option. #!/ usr / bin / jjs -cp ./some/lib/lib.jar将返回以下错误消息: “ -cp ./some/lib/lib.jar”不是可识别的选项。
  • #!/usr/bin/jjs -scripting -fv will return the error message: "-scripting -fv" is not a recognized option. #!/ usr / bin / jjs -scripting -fv将返回错误消息: “ -scripting -fv”不是公认的选项。 Use "-h" or "-help" to see a list of all supported options" 使用“ -h”或“ -help”查看所有支持的选项的列表”

All options are theoretically valid. 所有选项在理论上都是有效的。 The classpath option should also works as seen on http://www.adam-bien.com/roller/abien/entry/setting_the_classpath_for_nashorn . http://www.adam-bien.com/roller/abien/entry/setting_the_classpath_for_nashorn所示,classpath选项也应起作用。 More info about nashorn and Shebang: http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html#CHDEGHJJ 有关nashorn和Shebang的更多信息: http ://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html#CHDEGHJJ

You ran into an issue which doesn't have anything to do with Nashorn nor Java. 您遇到了与Nashorn或Java无关的问题。 According to this answer the command line argument handling with shebang never was clearly specified and it seems to be a common behavior to treat everything encountered after the first white-space as one single argument. 根据此答案 ,从未明确指定使用shebang进行命令行参数处理,并且将第一个空格之后遇到的所有内容视为一个单独的参数似乎是一种常见的行为。

So one solution would be to write a shell script containing an invocation of jjs with the actual arguments and use that shell script as the interpreter in the shebang line of your Nashorn script. 因此,一种解决方案是编写一个包含实际参数的jjs调用的shell脚本,并将该shell脚本用作Nashorn脚本的shebang行中的解释器。

You can use -Dnashorn.args=-cp in a shebang script. 您可以在shebang脚本中使用-Dnashorn.args = -cp。 See also https://bugs.openjdk.java.net/browse/JDK-8072138 另请参见https://bugs.openjdk.java.net/browse/JDK-8072138

As it appears you want a way to automatically add JARs to your classpath, I'll highlight a little wrapper I've written which allows you to define Maven co-ordinate dependencies (including transitives) to be added to the classpath of your script, so you can write a script using the "# dep" lines: 在您看来,您需要一种自动将JAR添加到类路径的方法时,我将重点介绍我编写的一个包装器,该包装器允许您定义要添加到脚本的类路径的Maven坐标依赖项(包括传递对象),因此您可以使用“#dep”行编写脚本:

#!/usr/bin/env jjs-with-deps
#
# The line below is parsed by the jjs-with-deps script to build a new
# classloader in which the script is really executed, including logback
# and its transitive dependencies.
#
# dep:ch.qos.logback:logback-classic:1.1.2

var log = org.slf4j.LoggerFactory.getLogger("com.example.app.Logger");
log.info("Hello World!");

It does require Maven to be installed somewhere on your PATH, and it does slightly increase startup time (but then again, you're already starting up a JVM ;). 它确实需要将Maven安装在PATH上的某个位置,并且确实会稍微增加启动时间(但同样,您已经在启动JVM;)。 First invocation of a given script will be much slower while any dependencies are downloaded to the local M2 repository. 将任何依赖项下载到本地M2存储库时,给定脚本的首次调用会慢得多。

Link is https://github.com/stevestorey/jjs-with-deps 链接是https://github.com/stevestorey/jjs-with-deps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM