标签: java-7

JDK7:钻石推理语法混乱

尝试在JDK7中编译以下代码:

import java.nio.file.*;

public final class _DiamondSyntaxErrors {
  public interface InterfaceA<T> {
  }

  public abstract static class ClassA<T>
      implements InterfaceA<T> {
    protected ClassA() {
    }
  }

  public static void main(String... args) {
    // no error
    InterfaceA<Path> classA = new ClassA<>() {
    };

    // error: cannot infer type arguments for SimpleFileVisitor<>
    FileVisitor<Path> visitor = new SimpleFileVisitor<>() {
    };
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么钻石语法的第二次使用不起作用?

第一次使用有什么大不同?

java type-inference diamond-operator java-7

8
推荐指数
1
解决办法
2240
查看次数

Java 7:COM-API不适用于Quality Center(OTAClient.dll),但适用于Java 6

我们在Windows 7 Enterprise,SP1,64bit上运行.我们刚刚在所有机器上安装了Java 7,这会导致以下问题:

当我的程序尝试从Quality Center与OpenTestArchitecture-API进行通信时,无法创建ActiveX-Component.我使用JACOB作为我的Java-COM库.升级到最新版本的JACOB并没有改变任何东西.

以下测试适用于jdk1.6.0_24(32位)但使用jdk1.7.0_04(32位)失败:

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.LibraryLoader;

public class JacobTest {
    static {
        File lib = new File("lib/" + LibraryLoader.getPreferredDLLName() + ".dll");
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, lib.getAbsolutePath());

        System.out.println("JACOB_DLL_PATH = " + lib.getAbsolutePath());
        LibraryLoader.loadJacobLibrary();
    }

    public static void main(String[] args) {
        try {
            // Excel: Works with jdk1.6.0_24 AND jdk1.7.0_04
            System.out.println("new ActiveXComponent(\"Excel.Application\");");
            new ActiveXComponent("Excel.Application");

            // Quality Center OTAClient: Only works with jdk1.6.0_24
            System.out.println("ActiveXComponent component = new ActiveXComponent(\"TDApiOle80.TDConnection\");");
            ActiveXComponent component = new ActiveXComponent("TDApiOle80.TDConnection"); …
Run Code Online (Sandbox Code Playgroud)

java com jacob java-7 hp-quality-center

8
推荐指数
1
解决办法
4500
查看次数

由于缺少GenericArrayType而从Java6升级到Java7时出现ClassCastException

我的代码在Java 5和6中运行,但是当我升级到Java 7时,我得到一个"java.lang.ClassCastException:java.lang.Class不能转换为java.lang.reflect.GenericArrayType".

对于以下代码:

public class A<T> {
  public Vector<Integer[]> arr;
}

System.out.println(((ParameterizedType)A.class.getField("arr").getGenericType()).getActualTypeArguments()[0]);
System.out.println(((ParameterizedType) A.class.getField("arr").getGenericType()).getActualTypeArguments()[0].getClass());
Run Code Online (Sandbox Code Playgroud)

Java6打印:

java.lang.Integer[]
class sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl
Run Code Online (Sandbox Code Playgroud)

Java7打印:

class [Ljava.lang.Integer;
class java.lang.Class
Run Code Online (Sandbox Code Playgroud)

这种不同的行为破坏了我依赖于泛型的代码.是否有人可以帮助我理解为什么会这样,以及如何获得GenericArrayType从Vector<Integer[]> arr?

java generics java-7

8
推荐指数
1
解决办法
999
查看次数

用于多个目录的Java nio WatchService

我想使用Java NIO观察(监视)多个目录 WatchService.我的问题是要监视的目录数是动态的,用户可以添加任意数量的目录WatchService.这可以实现吗?

java nio java-7

8
推荐指数
1
解决办法
6053
查看次数

为什么InetAddress.getLocalHost()的这种用法适用于Java 6但在OSX上使用Java 7失败

它适用于我,但在特定客户的OSX Mavericks上,InetAddress.getLocalHost()在Java 7中失败,尽管它在Java 6中运行正常,但有以下异常

java.net.UnknownHostException: rupert: rupert: nodename nor servname provided, or not known
    at java.net.InetAddress.getLocalHost(InetAddress.java:1466)
Caused by: java.net.UnknownHostException: rupert: nodename nor servname provided, or not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:894)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1286)
    at java.net.InetAddress.getLocalHost(InetAddress.java:1462)
Run Code Online (Sandbox Code Playgroud)

这是设计的吗?

更新

看起来是Java 8中修复的已知错误

https://bugs.openjdk.java.net/browse/JDK-7180​​557

java macos java-7 osx-mavericks

8
推荐指数
2
解决办法
8990
查看次数

JAXB混合版本?undefined'required'属性

我使用wsimport maven插件生成一些来自WSDL文件的类@ Mule Anypoint Studio 3.5 with JDK 1.7_55

我正在使用jaxb 2.2.7并从mule libs中删除2.1.9版本并替换为2.2.7.

当我编译时,sometines工作正常,但其他我多次采取此错误:

The attribute required is undefined for the annotation type XmlElementRef   
Run Code Online (Sandbox Code Playgroud)

我试图在JDK中创建一个支持的文件夹,并包含.jars,

你知道如何避免这个错误或正确替换这个库吗?

我在pom.xml中包含了这个依赖项

<dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-tools</artifactId>
        <version>2.2.7</version>
    </dependency>

    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
    <!-- xjc -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-xjc</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>idlj-maven-plugin</artifactId>
        <version>1.2.1</version>
    </dependency>       
Run Code Online (Sandbox Code Playgroud)

wsimport是2.2.7到

wsimport设置:

<plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>wsdl-AMANSequenceService-exec</id>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <args>
                            <arg>-version</arg>
                            <arg>-B-nv</arg>
                            <arg>-Xdebug</arg>
                            <arg>-B-XautoNameResolution</arg>
                            <arg>-Xendorsed</arg>
                        </args>
                        <extension>true</extension>
                        <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
                        <destDir>${basedir}/src/main/java</destDir>
                        <extension>true</extension>
                        <wsdlDirectory>${basedir}/src/main/resources/SICG/AMANSequenceService</wsdlDirectory>
                        <wsdlFiles> …
Run Code Online (Sandbox Code Playgroud)

java jax-ws jaxb mule java-7

8
推荐指数
1
解决办法
1万
查看次数

为什么这在Java7中编译而在Java8中不编译?

泛型是棘手的.看起来它们在不同版本的Java中被区别对待.

此代码在Java 7中成功编译,无法使用Java 8进行编译.

import java.util.EnumSet;

public class Main {
  public static void main(String[] args) {
    Enum foo = null;
    tryCompile(EnumSet.of(foo));
  }

  static <C extends Enum<C> & Another> void tryCompile(Iterable<C> i) {}

  static interface Another {}
}
Run Code Online (Sandbox Code Playgroud)

这是来自Java 8的错误消息.我用这个来编译它:http://www.compilejava.net/

/tmp/java_A7GNRg/Main.java:6: error: method tryCompile in class Main cannot be applied to given types;
    tryCompile(EnumSet.of(foo));
    ^
  required: Iterable<C>
  found: EnumSet
  reason: inferred type does not conform to upper bound(s)
    inferred: Enum
    upper bound(s): Enum<Enum>,Another
  where C is a type-variable: …
Run Code Online (Sandbox Code Playgroud)

java generics compiler-errors java-7 java-8

8
推荐指数
1
解决办法
2119
查看次数

try-with-resources用InputStreamReader包装流的地方?

我可能会过度思考这个问题,但我只是写了代码:

try (InputStream in = ModelCodeGenerator.class.getClassLoader().getResourceAsStream("/model.java.txt"))
{
    modelTemplate = new SimpleTemplate(CharStreams.toString(new InputStreamReader(in, "ascii")));
}
Run Code Online (Sandbox Code Playgroud)

这意味着InputStreamReader永远不会关闭(但在这种情况下,我们知道它的close方法只是关闭底层的InputStream.)

人们可以把它写成:

try (InputStreamReader reader = new InputStreamReader(...))
Run Code Online (Sandbox Code Playgroud)

但这似乎更糟糕.如果由于某种原因抛出InputStreamReader,则InputStream将永远不会被关闭,对吧?这是C++中的常见问题,其中构造函数调用其他构造函数.异常可能导致内存/资源泄漏.

这里有最好的做法吗?

java java-7

8
推荐指数
2
解决办法
8006
查看次数

-Xmx是一个硬限制?

这个SO答案澄清了有关-XmxJVM标志的一些事情.试着尝试我做了以下事情:

import java.util.List;
import java.util.ArrayList;

public class FooMain {

    private static String memoryMsg() {
        return String.format("%s. %s. %s"
                             , String.format("total memory is: [%d]",Runtime.getRuntime().totalMemory())
                             , String.format("free memory is: [%d]",Runtime.getRuntime().freeMemory())
                             , String.format("max memory is: [%d]",Runtime.getRuntime().maxMemory()));
    }

    public static void main(String args[]) {
        String msg = null;
        try {
            System.out.println(memoryMsg());
            List<Object> xs = new ArrayList<>();
            int i = 0 ;
            while (true) {
                xs.add(new byte[1000]);
                msg = String.format("%d 1k arrays added.\n%s.\n"
                                    ,  ++i
                                    , memoryMsg());
            }
        } finally { …
Run Code Online (Sandbox Code Playgroud)

java java-7

8
推荐指数
2
解决办法
1943
查看次数

杰克逊无法在Android上加载JDK7类型

我在我的Android应用程序中使用Jackson 2.8.2来反序列化JSON.反序列化本身可行,但是,我可以在应用程序日志中看到以下警告:

Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
Run Code Online (Sandbox Code Playgroud)

Proguard被禁用,sourceCompatibility设置为'1.7'.如何在构建中添加这些看似缺失的类型?

java android jackson java-7 kotlin

8
推荐指数
1
解决办法
9783
查看次数