一、问题场景
在开发时需要将服务器jar包拿到本地进行运行(配置文件在nacos),在windows11 cmd 中运行与服务器同样命令 java -jar xx.jar 时抛错。
二、具体报错
via location 'nacos:xxxx.yaml?refresh=true' cannot be found at org.springframework.boot.context.config.ConfigDataResourceNotFoundException.withLocation(ConfigDataResourceNotFoundException.java:97) at org.springframework.boot.context.config.ConfigDataImporter.handle(ConfigDataImporter.java:145) at org.springframework.boot.context.config.ConfigDataImporter.load(ConfigDataImporter.java:136) at org.springframework.boot.context.config.ConfigDataImporter.resolveAndLoad(ConfigDataImporter.java:86) at org.springframework.boot.context.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:115) at org.springframework.boot.context.config.ConfigDataEnvironment.processWithProfiles(ConfigDataEnvironment.java:313) at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:234) at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:96) at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:89) at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:109) at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:94) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136) at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:81) at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:64) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118) at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112) at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:63) at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:352) at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) at com.sddep.dtp.user.UserApplication.main(UserApplication.java:18) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:95) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65)Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1 at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:211) at org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:169) at org.yaml.snakeyaml.reader.StreamReader.prefix(StreamReader.java:141) at org.yaml.snakeyaml.scanner.ScannerImpl.scanPlainSpaces(ScannerImpl.java:2142) at org.yaml.snakeyaml.scanner.ScannerImpl.scanPlain(ScannerImpl.java:2077) at org.yaml.snakeyaml.scanner.ScannerImpl.fetchPlain(ScannerImpl.java:1099) at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:457) at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:263) at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingValue.produce(ParserImpl.java:694) at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:185) at org.yaml.snakeyaml.comments.CommentEventsCollector$1.peek(CommentEventsCollector.java:57) at org.yaml.snakeyaml.comments.CommentEventsCollector$1.peek(CommentEventsCollector.java:43) at org.yaml.snakeyaml.comments.CommentEventsCollector.collectEvents(CommentEventsCollector.java:136) at org.yaml.snakeyaml.comments.CommentEventsCollector.collectEvents(CommentEventsCollector.java:116) at org.yaml.snakeyaml.composer.Composer.composeScalarNode(Composer.java:239) at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:208) at org.yaml.snakeyaml.composer.Composer.composeKeyNode(Composer.java:347) at org.yaml.snakeyaml.composer.Composer.composeMappingChildren(Composer.java:332) at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:311) at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:212) at org.yaml.snakeyaml.composer.Composer.composeValueNode(Composer.java:357) at org.yaml.snakeyaml.composer.Composer.composeMappingChildren(Composer.java:336) at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:311) at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:212) at org.yaml.snakeyaml.composer.Composer.composeValueNode(Composer.java:357) at org.yaml.snakeyaml.composer.Composer.composeMappingChildren(Composer.java:336) at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:311) at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:212) at org.yaml.snakeyaml.composer.Composer.getNode(Composer.java:134) at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:168) at org.springframework.boot.env.OriginTrackedYamlLoader$OriginTrackingConstructor.getData(OriginTrackedYamlLoader.java:98) at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:499) at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:199) at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:166) at org.springframework.boot.env.OriginTrackedYamlLoader.load(OriginTrackedYamlLoader.java:83) at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:50) at com.alibaba.cloud.nacos.parser.NacosDataParserHandler.parseNacosData(NacosDataParserHandler.java:92) at com.alibaba.cloud.nacos.configdata.NacosConfigDataLoader.pullConfig(NacosConfigDataLoader.java:145) at com.alibaba.cloud.nacos.configdata.NacosConfigDataLoader.doLoad(NacosConfigDataLoader.java:82) at com.alibaba.cloud.nacos.configdata.NacosConfigDataLoader.load(NacosConfigDataLoader.java:69) at com.alibaba.cloud.nacos.configdata.NacosConfigDataLoader.load(NacosConfigDataLoader.java:58) at org.springframework.boot.context.config.ConfigDataLoaders.load(ConfigDataLoaders.java:96) at org.springframework.boot.context.config.ConfigDataImporter.load(ConfigDataImporter.java:128) ... 32 common frames omittedCaused by: java.nio.charset.MalformedInputException: Input length = 1 at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274) at java.base/sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at java.base/sun.nio.cs.StreamDecoder.read(StreamDecoder.java:188) at java.base/java.io.InputStreamReader.read(InputStreamReader.java:177) at org.yaml.snakeyaml.reader.UnicodeReader.read(UnicodeReader.java:118) at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:176) ... 74 common frames omitted
三、问题排查
1. jdk版本一致
2.运行命令一致
3.路径编码无中文
四、原因分析
Windows下运行jar读取Nacos配置文件乱码导致启动失败
Linux 默认编码为 UTF-8 (65001) 、Windows 默认编码 GBK(936)
五、解决方法
方案①:添加启动参数
添加参数 -Dfile.encoding=utf-8 ,如果是多jdk环境,考虑切换到jdk路径下使用./java 运行
java -jar -Dfile.encoding=utf-8 "xxx.jar"
如果报错 Error: Unable to access jarfile .encoding=utf-8
说明缺少系统环境变量
方案②:修改系统默认编码
cmd 运行 chcp 查看当前编码:
修改当前窗口编码:
或者
修改全局编码:
到达 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor 目录下
右键——新建——字符串值
重命名:autorun
双击修改数值数据
最后直接运行
java -jar xx.jar 即可