Java jdk Docker 中运行脚本

环境信息说明

  • Docker 镜像 majiajue/jdk1.8

以下示例记录实现获取百度云账户余额演示简单 Java 脚本在 Docker 容器中运行的过程和错误

运行 docker 容器

docker run -it majiajue/jdk1.8 bash

在容器中下载 SDK 并将其放在 /app/ 目录下,如 /app/bce-java-sdk-0.10.351/lib/bce-java-sdk-0.10.351.jar,为了让 JAVA 能找到此 JAR 文件,在容器路径 ~/.bashrc 中添加自定义的 Jar 文件路径

~/.bashrc
export CLASSPATH=$CLASSPATH:/app/bce-java-sdk-0.10.351/lib/bce-java-sdk-0.10.351.jar:/app/bce-java-sdk-0.10.351/lib/guava-33.4.0-jre.jar:/app/bce-java-sdk-0.10.351/third-party/*

获取余额的代码如下,容器中代码路径为 /app/BalanceQuery.java

/app/BalanceQuery.java
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.billing.BillingClient;
import com.baidubce.services.billing.BillingClientConfiguration;
import com.baidubce.services.billing.model.finance.SupervisorBalanceQueryRequest;
import com.baidubce.services.billing.model.finance.UserBalanceQueryRequest;
import com.baidubce.services.billing.model.finance.UserBalanceQueryResponse;
import com.baidubce.services.billing.model.finance.SupervisorBalanceResponse;
import java.util.Arrays;

public class BalanceQuery {

public static void main(String[] args) {
String accessKeyId = "ALTAKINjySCAAAAAAAAAAAAAAAAAAAAAAAA";
String secretAccessKey = "5290cfc18BBBBBBBBBBBBBBBBBBBBB";

BillingClientConfiguration config = new BillingClientConfiguration();
config.setCredentials(new DefaultBceCredentials(accessKeyId, secretAccessKey));
config.setEndpoint("https://billing.baidubce.com");

BillingClient client = new BillingClient(config);

try {

UserBalanceQueryResponse response = client.userBalanceQuery();

System.out.println("Balance: " + response.getCashBalance());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed");
}
}
}

在容器中(/app/)编译执行

如果未在容器环境变量中添加自定义的 JAR 路径(如 /app/bce-java-sdk-0.10.351/lib/bce-java-sdk-0.10.351.jar:/app/bce-java-sdk-0.10.351/lib/guava-33.4.0-jre.jar:/app/bce-java-sdk-0.10.351/third-party/*) 需要在编译(javac) 和执行 (java)时使用参数 -cp "/app/bce-java-sdk-0.10.351/lib/bce-java-sdk-0.10.351.jar:/app/bce-java-sdk-0.10.351/lib/guava-33.4.0-jre.jar:/app/bce-java-sdk-0.10.351/third-party/*:." 指定自定义文件路径,否则会导致某些类找不到

需要注意 -cp 选项中需要包含当前目录 . ,否则会导致找不到编译后的脚本文件,报错: Error: Could not find or load main class BalanceQuery

# cd /app/

# javac BalanceQuery.java

# java BalanceQuery
Balance: 1999.5100

java -cp "./bce-java-sdk-0.10.351/lib/bce-java-sdk-0.10.351.jar:./bce-java-sdk-0.10.351/third-party/*:." BalanceQuery
Balance: 1999.5100

如需在宿主机(容器外)执行脚本,参考以下命令:

# docker compose exec -it java_jdk_1.8 sh -c 'cd /app/;java -cp "./bce-java-sdk-0.10.351/lib/bce-java-sdk-0.10.351.jar:./bce-java-sdk-0.10.351/third-party/*:." BalanceQuery'

Balance: 1999.5100

查看 JAR 中包含的类

有时候需要查看 JAR 中包含的类的路径,可以在 Shell 中直接使用 unzip 命令对 JAR 文件进行解压,解压后即可看到具体的类(*.class)的路径

# unzip bce-java-sdk-0.10.351.jar

# ls /opt/com/baidubce/services/billing/model/finance/
SupervisorBalance.class SupervisorBalanceTransferRequest.class SupervisorTransactionResponse.class UserBalanceQueryResponse.class
SupervisorBalanceQueryRequest.class SupervisorTransaction.class TransferResultResponse.class
SupervisorBalanceResponse.class SupervisorTransactionPageRequest.class UserBalanceQueryRequest.class

查看 Java class 类文件中包含的方法

要在 Shell 查看 .class 编译类中包含的方法,可以参考以下代码实现:

ListMethods.java
import java.lang.reflect.Method;
import com.baidubce.services.billing.BillingClient;
// 导入要查看其方法的类
import com.baidubce.services.billing.model.finance.SupervisorBalanceResponse;

public class ListMethods {
public static void main(String[] args) {
Class<?> clazz = SupervisorBalanceResponse.class;

Method[] methods = clazz.getMethods();

for (Method method : methods) {
System.out.println(method.getName());
}
}
}

编译并查看结果:

# javac ListMethods.java

# java ListMethods
getResult
setResult
getMetadata
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll

反编译 class 文件查看其具体内容

使用 javac 编译后的 .class 文件无法使用编辑器直接查看,如果要查看其具体内容,需要对其进行反编译(由 .class 转换成可读的 .java 文件),可以参考以下方法:

  1. 下载 jd-cli.jar。通过 Github 页面 找到 Releases 页面下载 jd-cli-1.2.0-dist.tar.gz 并解压后即可得到 jd-cli.jar
  2. 使用如下命令反编译 .class 文件
    # java -jar jd-cli.jar /opt/com/baidubce/services/billing/model/finance/SupervisorBalanceResponse.class 
    09:47:45.792 INFO com.github.kwart.jd.cli.Main - Decompiling /opt/com/baidubce/services/billing/model/finance/SupervisorBalanceResponse.class
    package .opt.com.baidubce.services.billing.model.finance;

    import com.baidubce.model.AbstractBceResponse;
    import com.baidubce.services.billing.model.finance.SupervisorBalance;
    import java.util.List;

    public class SupervisorBalanceResponse extends AbstractBceResponse {
    private List<SupervisorBalance> result;

    public List<SupervisorBalance> getResult() {
    return this.result;
    }

    public void setResult(List<SupervisorBalance> result) {
    this.result = result;
    }
    }