入口代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | @SpringBootApplication public class SpringDemoApplication implements CommandLineRunner { private static final String arg = "wooden.businessPlatform.DownloadAndParse"; private final ConfigurableApplicationContext context; public SpringDemoApplication(ConfigurableApplicationContext context) { this.context = context; } public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } public void run(String... args) throws Exception { if (args != null && args.length != 0) { System.out.print("Bearer "); Arrays.stream(args).forEach(System.out::println); } else { System.out.println("Please import test token !"); } Object target = Class.forName("DownloadAndParse").newInstance(); if (target instanceof Executable) { Executable executable = (Executable) target; executable.execute(args[0]); } context.close(); } } |
越界异常如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | Exception in thread “main”java.lang.reflect.InvocationTargetException at sun.reflect.NativellethodAccessorImpl.invoke0(Native ethod) at sun.reflect.NativellethodAccessorImpl.invoke(Unknow Source) at sun.reflect.DelegatingllethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.boot.loader.MainllethodRunner.run(MainethodRurner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) Caused by:java.lang.IllegalStateException:Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.cal1Runner(SpringApplication.java:794) at org.springframework.boot.SpringApplication.cal1Runners(SpringApplication.java:775) at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) at example.SpringDemoApplication.main(SpringDemoApplication.java:31) ... 8 more Caused by: java.lang.ArrayIndexQutOfBoundsException: 1 at wooden.businessPlatform.metaDataDowmload.MetaDowload.DownloadMethod( letaDownload.java:36) at wooden.businessPlatform.DownloadAndParse.execute(DownloadAndParse.java:20) at example.SpringDemoApplication.run(SpringDemoApplication.java:42) at org.springframework.boot.SpringApplication.cal1Runner(SpringApplication.java:791) ... 13 more |
一开始以为是入口参数传递问题,后查看日志发现报错源自DownloadMethod.java
Debug之后定位问题为编码不匹配
原因是Jar需要按中文冒号:切割一串字符,取第二个元素
而在Windows cmd执行Jar包时,默认编码不是UTF8
变成乱码之后切割不开,导致数组下标越界
所以在WIN执行的时候要带上编码:
1 | java -jar -Dfile.encoding=utf-8 BizMetaDownAndParse-1.0-SNAPSHOT.jar |