Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.ClassCastException:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl不能强制转换为javax.net.ssl.HttpsURLConnection_Java_Eclipse_Maven_Netbeans_Classnotfoundexception - Fatal编程技术网

java.lang.ClassCastException:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl不能强制转换为javax.net.ssl.HttpsURLConnection

java.lang.ClassCastException:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl不能强制转换为javax.net.ssl.HttpsURLConnection,java,eclipse,maven,netbeans,classnotfoundexception,Java,Eclipse,Maven,Netbeans,Classnotfoundexception,我在我的Netbeans 7.4中尝试了这段代码,它可以正常工作 import java.io.IOException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class JavaApplication148 { public static void main(String[] args) throws IOException { URL url = new URL("h

我在我的Netbeans 7.4中尝试了这段代码,它可以正常工作

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public class JavaApplication148 {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://redmine.ackee.cz/time_entries.xml");
        HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection();
    }
}
然而,我在我的maven驱动项目的eclipse中使用了它,它引发了以下异常:

java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl 
cannot be cast to javax.net.ssl.HttpsURLConnection
下面是我的maven项目中抛出错误的类

package cz.ackee.redmine.commands;

import java.io.IOException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;

public abstract class RedmineCommand {      
    public void write(String xmlAddress, String text, String requestMethod) throws IOException{         
         URL url = new URL("https://redmine.xxx.cz/time_entries.xml");
         HttpsURLConnection httpCon = (HttpsURLConnection) url.openConnection(); //this line throws exception
    }       
}
这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cz.ackee</groupId>
    <artifactId>pan-unicorn-bot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Pan Unicorn Bot</name>
    <description>Pan unicorn IRC Bot</description>

  <repositories>
  <repository>
    <id>apache-snapshots</id>
    <url>http://repository.apache.org/snapshots/</url>
  </repository>
  </repositories>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.3-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.taskadapter</groupId>
            <artifactId>redmine-java-api</artifactId>
            <version>1.23</version>
        </dependency>
    </dependencies>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>/src/main/assembly/binary.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <index>true</index>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>cz.ackee.unicorn.PanUnicornMain</mainClass>
                        </manifest>
                        <manifestEntries>
                            <mode>development</mode>
                            <url>${project.url}</url>
                            <key>value</key>
                        </manifestEntries>
                    </archive>
                </configuration>

            </plugin>
        </plugins>
    </build>

</project>

4.0.0
cz.ackee
泛独角兽机器人
0.0.1-快照
泛独角兽机器人
泛独角兽IRC机器人
apache快照
http://repository.apache.org/snapshots/
mysql
mysql连接器java
5.1.30
org.hibernate
冬眠核心
4.3.5.最终版本
org.apache.commons
commons cli
1.3-1
com.taskadapter
RedmineJavaAPI
1.23
UTF-8
UTF-8
maven编译器插件
org.apache.maven.plugins
2.3
1.7
1.7
真的
真的
maven汇编插件
2.2.1
/src/main/assembly/binary.xml
组装
包裹
单一的
org.apache.maven.plugins
maven jar插件
2.3.2
真的
真的
cz.ackee.unicorn.PanUnicornMain
发展
${project.url}
价值
你知道为什么它在netbeans上编译和运行没有问题,为什么它在maven eclipse项目上运行不好吗


(我使用mvn包通过命令行编译,我通过eclipse运行它)

解决方案是更改这一行

URL url = new URL("https://redmine.xxx.cz/time_entries.xml");
进入这一行

URL url = new URL(null, "https://redmine.xxx.cz/time_entries.xml", new sun.net.www.protocol.https.Handler());

如果您只是使用
HTTP
协议(而不是
HTTPS
),而不是:

HttpsURLConnection httpCon=(HttpsURLConnection) openConnection()

使用以下命令:

HttpURLConnection httpCon=(HttpURLConnection)url.openConnection()


只要在“HttpsURLConnection”中删除“s

,如果url以“https”开头,它就会调用

使用netty-3.9.2>.jar解决此问题 当您使用这个netty.jar时。电话回访的过程发生了变化


为了避免此类错误,您可以对http和https url使用HttpUrlConnection(java.net.HttpUrlConnection)

HttpsUrlConnection仅添加“可用于”https连接的额外方法,但您可能不会使用这些方法

HttpsUrlConnection和HttpUrlConnection都是从UrlConnection派生的,UrlConnection包含允许读取响应的方法,如getInputStream()。HttpsUrlConnection派生自HttpUrlConnection,HttpUrlConnection派生自UrlConnection

java.net.HttpUrlConnection只需添加与http协议相关的方法(这些方法适用于https)

因此,您可以这样做:

URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
您也可以这样做并获得相同的结果,连接将是相同的:

URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

我猜你的classpath有点不同。classpath到底是什么?我在最新的luna eclipse中遇到了同样的异常。您的解决方案对我有效。请不要使用此解决方案。这是一种使用特定处理程序实现的变通方法。这可能会阻止代码在不同的JRE/JDK(供应商和/或版本)上运行。更多信息:虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。只有链接的答案可能会变得无效,如果链接页面更改,因为这是我正在寻找的。但这仅在协议为http
http
时有效,例如:
http://www.mywebsite.com
(注意
http
),而不是在安全协议
https
(如OP案例)中。正如我在回答中所解释的,HttpURLConnection对https url也是有效的
URL url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));