IDEA启动项目正常,但是java -jar启动失败

在Windows上部署项目时遇到一个问题,使用java-jar命令启动应用无法读取Nacos配置,导致报错。问题可能由JVM默认编码(可能是操作系统默认编码)引起,解决方案是在启动时添加-Dfile.encoding=UTF-8参数,确保JVM使用UTF-8编码,从而正确处理配置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天部署windows上的项目时遇到了个问题:在IDEA中能正常启动,但是只要用java -jar就没法读取nacos配置。

报错信息如下:总之就是读取不到nacos中的配置

2023-03-19 14:51:11,633 [main] WARN  [io.undertow.websockets.jsr] Bootstrap.java:68 - UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
2023-03-19 14:51:11,656 [main] INFO  [io.undertow.servlet] ServletContextImpl.java:382 - Initializing Spring embedded WebApplicationContext
2023-03-19 14:51:11,657 [main] INFO  [o.s.b.w.s.c.ServletWebServerApplicationContext] ServletWebServerApplicationContext.java:290 - Root WebApplicationContext: initialization completed in 1291 ms
2023-03-19 14:51:11,865 [main] WARN  [o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext] AbstractApplicationContext.java:591 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '*****Controller': Unsatisfied dependency expressed through field '*****Service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iceDynamicsServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'ice.iceModelFile.modelDirPath' in value "${ice.iceModelFile.modelDirPath}"
2023-03-19 14:51:11,880 [main] INFO  [o.s.b.a.l.ConditionEvaluationReportLoggingListener] ConditionEvaluationReportLoggingListener.java:136 - 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-03-19 14:51:11,902 [main] ERROR [org.springframework.boot.SpringApplication] SpringApplication.java:835 - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'iceDynamicsController': Unsatisfied dependency expressed through field '****Service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '*****ServiceImpl': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder '***.***.***' in value "${***.***.***}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
    at cn.*****.*****.model.FlModelApplication.main(FlModelApplication.java:26)
    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: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)

解决方式:启动时使用:java -jar -Dfile.encoding=UTF-8 ****.jar

原因:可能是由于当您运行 java -jar 命令时,JVM使用了不同的默认编码(默认情况下是操作系统默认编码)。这可能会导致应用程序无法正确地读取和处理配置信息等内容。

添加 -Dfile.encoding=UTF-8 参数是一种常见的解决方法,因为它将强制JVM使用UTF-8编码。这确保了应用程序可以正确地读取和处理文本数据。

<think>好的,我现在需要帮助用户了解如何在IntelliJ IDEA中使用java -jar命令运行JAR文件。首先,我要确保自己正确理解用户的问题。用户提到想配置和运行'shop-user.jar',所以需要指导他们在IDEA中如何操作。 根据提供的引用内容,尤其是引用[1]和[4],用户可能已经知道在命令行中使用java -jar,但需要在IDEA中设置。可能涉及几个步骤:配置运行参数、处理端口占用、确保依赖正确。我需要检查用户的引用内容是否有相关提示。 引用[1]提到要停止占用8080端口的项目,这可能是在运行jar前需要终止正在运行的进程。引用[3]提到循环依赖问题仅在直接运行jar时出现,这可能与依赖配置有关。引用[5]讲解了如何导入JAR依赖,这可能对配置项目结构有帮助。 接下来,我需要分步骤说明如何在IDEA中配置运行配置。首先,用户可能需要打开运行配置,添加一个JAR Application,指定jar路径,并设置JVM参数如-Dfile.encoding=UTF-8(如引用[4]所示)。另外,可能需要处理端口冲突,如停止其他占用8080端口的应用,如引用[1]所述。 另外,用户可能需要确保项目的依赖正确,特别是如果jar文件需要外部库。引用[5]提到了如何在Project Structure中添加JAR依赖,这可能相关。但这里用户是运行一个已生成的jar,而不是开发中的项目,所以可能不需要这一步,除非jar本身缺少依赖。 最后,可能需要考虑编码问题,如引用[4]中的-Dfile.encoding参数,确保控制台输出正确。同时,在运行前确保停止其他实例,避免端口冲突。总结步骤应该是:配置运行参数,处理依赖和端口,然后运行。</think>### 如何在 IntelliJ IDEA 中配置和运行 `java -jar shop-user.jar` 命令 #### 步骤 1:确保项目已正确打包为 JAR 文件 - 若 `shop-user.jar` 是通过 Maven/Gradle 生成的,需确认构建配置中包含 `spring-boot-maven-plugin` 或类似插件,确保生成可执行 JAR- 检查 `pom.xml` 或 `build.gradle` 文件中的打包配置,例如: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` [^3] #### 步骤 2:在 IntelliJ IDEA 中配置运行参数 1. **打开运行配置界面**: - 点击顶部菜单栏的 **Run** → **Edit Configurations**。 - 点击左上角 **+** 按钮,选择 **JAR Application**。 2. **配置 JAR 路径和参数**: - **Path to JAR**:选择 `shop-user.jar` 的本地路径。 - **Before launch**:若需预操作(如停止占用端口的进程),可添加 `Run Maven Goal` 或自定义脚本[^1]。 - **JVM 参数**(可选):添加 `-Dfile.encoding=UTF-8` 避免乱码[^4]。 ![JAR 配置示例](https://example.com/path/to/image) (注:此处仅为示意) #### 步骤 3:处理依赖和端口冲突 - **停止占用端口的进程**:若 `shop-user.jar` 使用 8080 端口,需先在 IDEA 中停止其他运行中的项目[^1]。 - **依赖问题**:若运行时报错 `循环依赖`,检查 Spring Boot 版本或使用 `@Lazy` 注解解决。 #### 步骤 4:执行运行命令 - 点击 IDEA 顶部工具栏的 **Run** 按钮(绿色箭头),或直接使用快捷键 `Shift+F10`。 - 控制台会输出启动日志,例如: ```log 2023-10-01 12:00:00.000 INFO com.example.ShopUserApplication - Started ShopUserApplication in 2.5 seconds ``` #### 替代方法:通过 Terminal 直接运行 -IDEA 内置终端(Alt+F12)中输入: ```bash java -jar shop-user.jar ``` 或指定编码(Windows): ```bash java -Dfile.encoding=UTF-8 -jar shop-user.jar ``` ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值