gpt4 book ai didi

java - 字节码是否知道 Java 版本

转载 作者:行者123 更新时间:2023-11-29 06:51:03 25 4
gpt4 key购买 nike

抱歉,如果这是一个愚蠢的问题,我还没有找到直接的答案。

我编译了一个使用 Java 9 特性编写的程序并生成了一个 .jar。

程序的本质是我需要它同时在两台机器上运行,一台运行 Java 8,一台运行 Java 9。我的程序不工作,特别是 jdk.incubator.httpclient 模块无法发送一个从一台机器到另一台机器的 HttpRequest。

将 JDK8 机器更新到 9 不是一个选项。

我的问题是,一旦它全部变成字节码还有什么关系吗? Java 9 生成的 jar 中是否存在 Java 8 JVM 无法理解的字节码?

可能值得注意的是,我必须使用 java -jar --add-modules=jdk.incubator.httpclient 运行 jar 才能使用新的 java 9 孵化器模块 httpclient

我知道一个聪明的解决方案是确保双方的防火墙都允许这两个人交谈,但我也想知道上述问题的答案!

提前致谢。

这是堆栈跟踪:

2018-01-09 01:15:57.410  INFO 4656 --- [nio-8080-exec-2] c.e.s.controller.MessageController       : [TO http://192.168.1.64:8080/send] Hello Bob it is Jim!
2018-01-09 01:18:08.551 ERROR 4656 --- [nio-8080-exec-2] c.e.s.service.MessagingService : send: unable to send request

java.net.ConnectException: Connection timed out
at java.base/sun.nio.ch.Net.connect0(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.connect(Net.java:454) ~[na:na]
at java.base/sun.nio.ch.Net.connect(Net.java:446) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648) ~[na:na]
at jdk.incubator.httpclient/jdk.incubator.http.PlainHttpConnection.connect(PlainHttpConnection.java:142) ~[jdk.incubator.httpclient:na]
at jdk.incubator.httpclient/jdk.incubator.http.Http1Exchange.sendHeadersOnly(Http1Exchange.java:136) ~[jdk.incubator.httpclient:na]
at jdk.incubator.httpclient/jdk.incubator.http.Exchange.responseImpl0(Exchange.java:298) ~[jdk.incubator.httpclient:na]
at jdk.incubator.httpclient/jdk.incubator.http.Exchange.responseImpl(Exchange.java:245) ~[jdk.incubator.httpclient:na]
at jdk.incubator.httpclient/jdk.incubator.http.Exchange.response(Exchange.java:121) ~[jdk.incubator.httpclient:na]
at jdk.incubator.httpclient/jdk.incubator.http.MultiExchange.response(MultiExchange.java:154) ~[jdk.incubator.httpclient:na]
at jdk.incubator.httpclient/jdk.incubator.http.HttpClientImpl.send(HttpClientImpl.java:234) ~[jdk.incubator.httpclient:na]
at com.example.simplemessenger/com.example.simplemessenger.service.MessagingService.send(MessagingService.java:30) ~[classes/:na]
at com.example.simplemessenger/com.example.simplemessenger.controller.MessageController.sendMessage(MessageController.java:71) [classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) [spring-web-4.3.13.RELEASE.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) [spring-web-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.13.RELEASE.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.13.RELEASE.jar:na]
at tomcat.embed.core@8.5.23/javax.servlet.http.HttpServlet.service(HttpServlet.java:661) [tomcat-embed-core-8.5.23.jar:na]
at spring.webmvc@4.3.13.RELEASE/org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.13.RELEASE.jar:na]
at tomcat.embed.core@8.5.23/javax.servlet.http.HttpServlet.service(HttpServlet.java:742) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.websocket@8.5.23/org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.13.RELEASE.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) [spring-web-4.3.13.RELEASE.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.13.RELEASE.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.13.RELEASE.jar:na]
at spring.web@4.3.13.RELEASE/org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.13.RELEASE.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:na]
at tomcat.embed.core@8.5.23/org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) [na:na]
at tomcat.embed.core@8.5.23/org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:na]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]

2018-01-09 01:18:08.552 INFO 4656 --- [nio-8080-exec-2] c.e.s.controller.MessageController : failed to send

最佳答案

答案是肯定的。工具 javap 可用于在 Java 8 上查看此内容

$ javap -v Hello.class | grep "version"
minor version: 0
major version: 52

在 Java 9 上,你会看到

  minor version: 0
major version: 53

但是,您可以使用编译器标志 -target-source 使 Java 9 编译器生成 Java 8 兼容代码。

$ javac -source 1.8 -target 1.8 Hello.java

但是,新的 incubator 可能还没有向后移植到 Java 1.8;在这种情况下,我建议最好的办法是使用 apache HttpComponents - 这应该对两者都有效。

关于java - 字节码是否知道 Java 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48160464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com