zno2

启动 springboot jar包时指定额外的classpath (-Xbootclasspath/a:)

https://www.cnblogs.com/zno2/p/16353014.html

借助 java -jar -Xbootclasspath/a:D:\conf testcp.jar

 

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FooApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(FooApplication.class, args);
        
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        grid(runtimeMXBean);
        
        try {
            Properties properties = new Properties();
            properties.load(FooApplication.class.getClassLoader().getResourceAsStream("sdk.properties"));
            grid(properties);
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void grid(RuntimeMXBean runtimeMXBean) {
        List<KV> list = new ArrayList<>();
        list.add(new KV("name",runtimeMXBean.getName()));
        list.add(new KV("vmName",runtimeMXBean.getVmName()));
        list.add(new KV("vmVendor",runtimeMXBean.getVmVendor()));
        list.add(new KV("vmVersion",runtimeMXBean.getVmVersion()));
        list.add(new KV("specName",runtimeMXBean.getSpecName()));
        list.add(new KV("specVendor",runtimeMXBean.getSpecVendor()));
        list.add(new KV("managementSpecVersion",runtimeMXBean.getManagementSpecVersion()));
        list.add(new KV("inputArguments",runtimeMXBean.getInputArguments().toString()));
        list.add(new KV("uptime",runtimeMXBean.getUptime()+""));
        list.add(new KV("startTime",runtimeMXBean.getStartTime()+""));
        grid(list);
        
        grid("bootClassPath", runtimeMXBean.getBootClassPath());
        grid("classPath", runtimeMXBean.getClassPath());
        grid("libraryPath", runtimeMXBean.getLibraryPath());
    }
    
    public static void grid(String k, String vs) {
        List<KV> list = new ArrayList<>();
        String[] splits = vs.split(";");
        for (String split : splits) {
            list.add(new KV(k,split));
        }
        grid(list);
    }
    
    public static void grid(Properties properties) {
        List<KV> list = new ArrayList<>();
        properties.forEach((k,v)->{
            list.add(new KV(k.toString(), v.toString()));
        });
        grid(list);
    }
    
    public static void grid(List<KV> list) {
        int aLen = 0;
        int bLen = 0;
        
        for (KV kv : list) {
            String k = kv.getK();
            String v = kv.getV();
            if(k.length() > aLen) {
                aLen = k.length();
            }
            if(v.length() > bLen) {
                bLen = v.length();
            }
        }
        
        System.out.println("|" + padding("",1+aLen+1+1+bLen, '-') + "|");
        for (KV kv : list) {
            String k = kv.getK();
            String v = kv.getV();
            System.out.println("| " + padding(k,aLen,' ') + "| " + padding(v,bLen,' ') + "|");
        }
        System.out.println("|" + padding("",1+aLen+1+1+bLen, '-') + "|");
        
    }

    public static String padding(String s, int length, char c) {
        String r = s;
        for(int i=0;i<length-s.length();i++) {
            r += c;
        }
        return r;
    }
    
    public static class KV {
        private String k;
        private String v;
        public KV(String k, String v) {
            this.k = k;
            this.v = v;
        }
        public String getK() {
            return k;
        }
        public void setK(String k) {
            this.k = k;
        }
        public String getV() {
            return v;
        }
        public void setV(String v) {
            this.v = v;
        }
        
    }
}

 

上面是springboot 项目,打jar包后测试如下:

首先创建目录D://conf

目录中添加 sdk.properties

上面代码会尝试在classpath中加载上面的文件

 java -jar  testcp.jar 

 

|----------------------------------------------------------|
| name                 | 22108@DESKTOP-5KL2IOV             |
| vmName               | Java HotSpot(TM) 64-Bit Server VM |
| vmVendor             | Oracle Corporation                |
| vmVersion            | 25.181-b13                        |
| specName             | Java Virtual Machine Specification|
| specVendor           | Oracle Corporation                |
| managementSpecVersion| 1.2                               |
| inputArguments       | []                                |
| uptime               | 1495                              |
| startTime            | 1666347906908                     |
|----------------------------------------------------------|
|-------------------------------------------------------------|
| bootClassPath| D:\Program Files\java\jre8\lib\resources.jar |
| bootClassPath| D:\Program Files\java\jre8\lib\rt.jar        |
| bootClassPath| D:\Program Files\java\jre8\lib\sunrsasign.jar|
| bootClassPath| D:\Program Files\java\jre8\lib\jsse.jar      |
| bootClassPath| D:\Program Files\java\jre8\lib\jce.jar       |
| bootClassPath| D:\Program Files\java\jre8\lib\charsets.jar  |
| bootClassPath| D:\Program Files\java\jre8\lib\jfr.jar       |
| bootClassPath| D:\Program Files\java\jre8\classes           |
|-------------------------------------------------------------|
|----------------------|
| classPath| testcp.jar|
|----------------------|
|--------------------------------------------------------------------------------------|
| libraryPath| C:\Program Files (x86)\Common Files\Oracle\Java\javapath                |
| libraryPath| C:\WINDOWS\Sun\Java\bin                                                 |
| libraryPath| C:\WINDOWS\system32                                                     |
| libraryPath| C:\WINDOWS                                                              |
| libraryPath| C:\Program Files (x86)\Common Files\Oracle\Java\javapath                |
| libraryPath| C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\|
| libraryPath| C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\      |
| libraryPath| C:\Windows\system32                                                     |
| libraryPath| C:\Windows                                                              |
| libraryPath| C:\Windows\System32\Wbem                                                |
| libraryPath| C:\Windows\System32\WindowsPowerShell\v1.0\                             |
| libraryPath| C:\Windows\System32\OpenSSH\                                            |
| libraryPath| C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL  |
| libraryPath| C:\Program Files\Intel\Intel(R) Management Engine Components\DAL        |
| libraryPath| C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT  |
| libraryPath| C:\Program Files\Intel\Intel(R) Management Engine Components\IPT        |
| libraryPath| C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common                  |
| libraryPath| C:\WINDOWS\system32                                                     |
| libraryPath| C:\WINDOWS                                                              |
| libraryPath| C:\WINDOWS\System32\Wbem                                                |
| libraryPath| C:\WINDOWS\System32\WindowsPowerShell\v1.0\                             |
| libraryPath| C:\WINDOWS\System32\OpenSSH\                                            |
| libraryPath| D:\e\Java\openjdk1.8.0_181\bin                                          |
| libraryPath| D:\e\maven\apache-maven-3.6.0\bin                                       |
| libraryPath| D:\Program Files\TortoiseGit\bin                                        |
| libraryPath| D:\Program Files\Git\cmd                                                |
| libraryPath| D:\Program Files\TortoiseSVN\bin                                        |
| libraryPath| D:\e\nodejs\node-v14.3.0-win-x86                                        |
| libraryPath| C:\Program Files (x86)\PuTTY\                                           |
| libraryPath| C:\Users\witas\AppData\Local\Microsoft\WindowsApps                      |
| libraryPath| D:\Program Files\OpenSSL-Win64\bin                                      |
| libraryPath|                                                                         |
| libraryPath| .                                                                       |
|--------------------------------------------------------------------------------------|
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Unknown Source)
        at java.util.Properties.load0(Unknown Source)
        at java.util.Properties.load(Unknown Source)
        at cn.xs.testcp.FooApplication.main(FooApplication.java:24)
        ... 8 more

 

 java -jar -Xbootclasspath/a:D:\conf testcp.jar 

 

 

小结:可以通过 -Xbootclasspath/a:<这里是你的目录> 来指定额外的classpath 

posted on 2023-06-02 08:19  zno2  阅读(266)  评论(0编辑  收藏  举报

导航