为什么在Java中我们可以捕获一个Exception即使它没有被抛出,但我们无法捕获它的子类(除了"unchecked" RuntimeException和它的子类).示例代码:
class Test {
public static void main(String[] args) {
try {
// do nothing
} catch (Exception e) {
// OK
}
try {
// do nothing
} catch (IOException e) {
// COMPILER ERROR: Unreachable catch block for IOException.
//This exception is never thrown from the try statement body
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在java和cxf中有应用程序,它使用客户端证书连接到WebServices.
我从WebService所有者那里获得了证书
我有问题,直接将此p12证书转换为java要求的工作jks密钥库.
我这样做了:
keytool -importkeystore -srckeystore certificate.p12 -srcstoretype PKCS12 -destkeystore certificate1.jks -deststoretype JKS -storepass secret
keytool -import -alias root -file root_ca.cer -trustcacerts -keystore certificate1.jks -storepass secret
keytool -import -alias trusted -file trusted_ca.cer -trustcacerts -keystore certificate1.jks -storepass secret
Run Code Online (Sandbox Code Playgroud)
但这个jks不起作用,我在使用这个certificate1.jks时得到HTTP响应'403:Forbidden'
但是,如果我将此p12(pfx)证书导入Internet Explorer,然后将此证书从IE导出为pfx格式,选择"在证书路径中包含所有证书"复选框并使用:
keytool -importkeystore -srckeystore certificate.pfx -srcstoretype PKCS12 -destkeystore certificate2.jks -deststoretype JKS -storepass secret
keytool -import -alias root -file root_ca_kir.cer -trustcacerts -keystore certificate2.jks -storepass secret
keytool -import -alias trusted -file trusted_ca_kir.cer -trustcacerts -keystore certificate2.jks -storepass secret …Run Code Online (Sandbox Code Playgroud) 我认为我对Java泛型有一些很好的理解.
这段代码没有编译,我知道为什么.
我们只能传递给测试方法类型Animal的类型或它的超类型(如对象列表)
package scjp.examples.generics.wildcards;
import java.util.ArrayList;
import java.util.List;
class Animal {}
class Mammal extends Animal {}
class Dog extends Mammal {}
public class Test {
public void test(List<? super Animal> col) {
col.add(new Animal());
col.add(new Mammal());
col.add(new Dog());
}
public static void main(String[] args) {
List<Animal> animalList = new ArrayList<Animal>();
List<Mammal> mammalList = new ArrayList<Mammal>();
List<Dog> dogList = new ArrayList<Dog>();
new Test().test(animalList);
new Test().test(mammalList); // Error: The method test(List<? super Animal>) in the type Test is not …Run Code Online (Sandbox Code Playgroud) 我有与Java内部类有关的问题.
有没有办法从顶级类Main访问顶级类A,定义内部类A?
以下是演示此问题的示例代码:
class A { // Outer Class A
{
System.out.println("A outer");
}
}
class B { // Outer Class B
{
System.out.println("B outer");
}
}
public class Main {
class A { // Inner Class A
{
System.out.println("A inner");
}
}
public void newA() {
class A { // Local Class A
{
System.out.println("A local");
}
}
new A();
}
public static void main(String[] args) {
new Main().newA(); // prints "A local"
new Main().new A(); // prints "A …Run Code Online (Sandbox Code Playgroud)