在Java Shebang脚本中加载库 [英] Load library in a java shebang script

查看:54
本文介绍了在Java Shebang脚本中加载库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自JDK-11起,我们就可以直接运行Java源代码.这段代码

Since JDK-11 we have ability to run java source code directly. This code

import org.apache.commons.codec.digest.Md5Crypt;

public class Oneliner {
  public static void main(String[] args){
    System.out.println(Md5Crypt.md5Crypt("ok".getBytes(), "$1$saltsalt"));
  }
}

可以与

$ /usr/lib/jvm/jdk-11/bin/java --source 8 -cp /home/imaskar/.m2/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar jscript.java

但是以shell脚本形式(shebang)

But in a shell script form (shebang)

#!/usr/lib/jvm/jdk-11/bin/java --source 8 --class-path /home/imaskar/.m2/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar

import org.apache.commons.codec.digest.Md5Crypt;

public class Oneliner {
  public static void main(String[] args){
    System.out.println(Md5Crypt.md5Crypt("ok".getBytes(), "$1$saltsalt"));
  }
}

我得到一个错误:

$ ./jscript.sh
Error: Could not find or load main class ..jscript.sh
Caused by: java.lang.ClassNotFoundException: //jscript/sh

问题是第一行中的--class-path参数.出于某些原因,--souce参数通过,但--class-path没有通过.

The problem is --class-path argument in the first line. For some reason --souce argument gets through, but --class-path doesn't.

推荐答案

在OpenJDK< = 11.0.7中,该通常不起作用.目前尚不清楚这是否是错误,是否已修复.有一个开放的错误报告:

This does not generally work in OpenJDK <= 11.0.7. Whether this is a bug and gets fixed or not is not clear yet. There is an open bug report:

https://bugs.openjdk.java.net/browse/JDK-8242911

指定--class-path至少与OpenJDK 12.0.214.0.1一起使用. 因此,我假设对Java 12进行了一些改进以解决此问题.

Specifying the --class-path works at least with OpenJDK 12.0.2 and 14.0.1. So I assume some improvements made for Java 12 fixed this issue.

因此问题中的行应该可以正常工作,无需更改:

So the line in the question is supposed to work, no change needed:

#!/usr/lib/jvm/jdk-11/bin/java --source 8 --class-path /home/imaskar/.m2/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar


关于其他答案和评论中提到的其他事项的一些注释:


Some notes on other things mentioned in the other answer and in the comments:

  • --source 必须为第一个参数.在shebang文件中,第一行被视为#!$COMMAND $ONE-SINGLE-ARGUMENT.因此,shell不会通过空格将$ONE-SINGLE-ARGUMENT分开.因此,如果Java启动器以--source开头,则会将其参数用空格分隔,然后进一步处理其他参数.
  • 我无法完全解释muttonUp的工作示例.我怀疑这与macOS的使用有关.也许用过的外壳已经分解了shebang参数.
  • 因此,此问题可能仅限于某些外壳.我已经用Ubuntu的bashdash测试了行为.
  • --source must be the first argument. In shebang files, the first line is treated like #!$COMMAND $ONE-SINGLE-ARGUMENT. So the shell will not separate the $ONE-SINGLE-ARGUMENT by whitespace. Thus, the Java launcher will split the argument by whitespace iff it starts with --source and further process the other arguments.
  • I can't fully explain muttonUp's working example. I suspect it is related to the usage of macOS. Maybe the used shell split up the shebang argument already.
  • Thus, it might be that this issue is limited to certain shells. I have tested the behavior with Ubuntu's bash and dash.

这篇关于在Java Shebang脚本中加载库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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