java.lang.Classcastexception-Fehler: com.Sonne.net.ssl.intern.www.Protokoll.https.HttpsURLConnectionOldImpl kann nicht umgewandelt werden, javax.net.ssl.HttpsURLConnection
Ich habe versucht, diesen code in meinem Netbeans 7.4 und es funktioniert ohne problem
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();
}
}
Aber dann habe ich es in eclipse in mein maven-Projekt getrieben und es wirft folgende exception :
java.lang.ClassCastException: com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl
cannot be cast to javax.net.ssl.HttpsURLConnection
Dies ist folgende Klasse in mein maven-Projekt, das wirft einen Fehler
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
}
}
Dies ist mein 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>
Irgendeine Idee, warum es nicht kompiliert und läuft ohne problem auf netbeans und warum es nicht gut gehen, auf maven-eclipse-Projekt?
(Ich kompilieren über Kommandozeile mit mvn package
betreibe ich es über eclipse)
- Ich würde vermuten, dass es einen Unterschied in den classpath.
- - Klassenpfad für was genau?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Die Lösung ist, ändern Sie diese Zeile
in dieser Zeile
Wenn Sie einfach
HTTP
- Protokoll (nichtHTTPS
), statt:Verwenden:
Legen Sie einfach das "s" in "HttpsURLConnection"
Beachten Sie, dass diese Lösung funktioniert nur für HTTP (nicht HTTPS)
http
zum Beispiel:http://www.mywebsite.com
(Hinweis diehttp
), und nicht in ein Sicheres Protokollhttps
(wie die OP-Fall).Es aufgerufen, wenn die url beginnt mit "https"
verwenden netty-3.9.2 > .jar um dieses Problem zu lösen
Wenn Sie diese netty.jar. der Prozess der post-Aufruf geändert.
http://dl.bintray.com/netty/downloads/netty-3.9.12.Final.tar.bz2
Zu vermeiden, diese Art von Fehler können Sie HttpUrlConnection (java.net.HttpUrlConnection) für http-und https-url
HttpsUrlConnection nur das hinzufügen von zusätzlichen Methoden, die benutzt werden kann" für die https-Verbindung, aber Sie wahrscheinlich nicht verwenden diese Methoden.
Beide HttpsUrlConnection und HttpUrlConnection abgeleitet von UrlConnection enthält Methoden, die erlauben, die Antwort Lesen, wie getInputStream(). HttpsUrlConnection abgeleitet von HttpUrlConnection, die abgeleitet von UrlConnection.
java.net.HttpUrlConnection nur hinzufügen, Methoden-relativ zum http-Protokoll (diese Verfahren gelten für https)
https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html
https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/HttpsURLConnection.html
Damit Sie dies tun können :
Wie gut können Sie dies tun und erhalten das gleiche Ergebnis, wird die Verbindung das gleiche :