为什么PropertyDescriptor行为从Java 1.6变为1.7?

更新:Oracle已将此确认为错误。 简介:在JDK 1.7中工作的某些自定义BeanInfo和PropertyDescriptor在JDK 1.7中失败,有些只在垃圾收集运行并且清除了某些SoftReferences后才会失败。 编辑:这也将破坏Spring 3.1中的ExtendedBeanInfo ,如post底部所述。 编辑:如果您调用JavaBeans规范的第7.1或8.3节,请准确说明规范的这些部分需要什么。 这些部分中的语言不是必要的或规范性的。 这些部分中的语言是示例的语言,这些语言最多不像规范那样含糊不清。 此外, BeanInfo API特别允许人们更改默认行为,并且在下面的第二个示例中明确区分了它。 Java Beans规范查找具有void返回类型的默认setter方法,但它允许通过java.beans.PropertyDescriptor自定义getter和setter方法。 使用它的最简单方法是指定getter和setter的名称。 new PropertyDescriptor(“foo”, MyClass.class, “getFoo”, “setFoo”); 这在JDK 1.5和JDK 1.6中有效,以指定setter名称,即使其返回类型不是void,如下面的测试用例中所示: import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import org.testng.annotations.*; /** * Shows what has worked up until JDK 1.7. */ public class PropertyDescriptorTest { private int i; public int getI() { return i; } […]

Webstart运行错误版本的JRE

我有一个需要Java 7+的Java webstart应用程序,所以JNLP有这一行: 这适用于各种Windows XP和Windows 7计算机(大多数还安装了Java 6),但在其中一台计算机上,webstart是使用Java 6启动的,并且失败了(错误版本的JRE)。 我发现这篇文章并检查了Java设置,它们看起来很好: 我也尝试将JNLP线更改为此但仍然无效: 关于如何让webstart了解JRE 1.7的任何想法? 配置:Windows 7,以管理员身份运行 – 从IE启动的webstart应用程序。

如何使用JUnit对JavaFX控制器进行unit testing

什么是初始化JavaFX运行时的正确方法,以便您可以使用并发工具和Platform.runLater(Runnable)进行unit testing(使用JUnit)控制器? 从@BeforeClass方法调用Application.launch(…)导致死锁。 如果未调用Application.launch(…)则抛出以下错误: java.lang.IllegalStateException: Toolkit not initialized at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:121) at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:116) at javafx.application.Platform.runLater(Platform.java:52) at javafx.concurrent.Task.runLater(Task.java:1042) at javafx.concurrent.Task.updateMessage(Task.java:987) at com.xyz.AudioSegmentExtractor.call(AudioSegmentExtractor.java:64) at com.xyz.CompletionControllerTest.setUp(CompletionControllerTest.java:69) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) […]

谁在java中调用main函数?

public static void main(String[] args) { boolean t=true; System.out.println(“Before return”); if(t) return; System.out.println(“not execute”); } 在上面的代码中,当使用return时,它应该返回调用main函数的函数。 究竟是谁调用了mainfunction?

我可以使用compareTo对整数和双精度值进行排序吗?

我可以使用compareTo对整数和双精度值进行排序吗? 我的系统给了我一个错误,我不能在原始类型int上调用compareTo(int)。 有任何想法吗? 码: public int compare(Object o1, Object o2) { Record o1C = (Record)o1; Record o2C = (Record)o2; return o1C.getPrice().compareTo(o2C.getPrice()); } class Record public class Record { String name; int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } […]

JVM如何在实践中收集SoftReferences?

我有一个在JVM中运行的两个单独的缓存(一个由第三方库控制),每个缓存都使用软引用。 我希望JVM在由库控制的缓存之前清除我的受控缓存。 SoftReference javadoc指出: 在虚拟机抛出OutOfMemoryError之前,保证已清除对软可访问对象的所有软引用。 否则,不会对清除软引用的时间或清除对不同对象的一组此类引用的顺序施加约束。 但是,鼓励虚拟机实现偏向清除最近创建或最近使用的软引用。 此类的直接实例可用于实现简单缓存; 此类或派生的子类也可用于更大的数据结构,以实现更复杂的高速缓存。 只要软引用的引用是强可达的,即实际上是在使用中,软引用就不会被清除。 因此,复杂的高速缓存可以例如通过保持对这些条目的强烈指示来防止其最近使用的条目被丢弃,留下剩余的条目由垃圾收集器决定丢弃。 常见的JVM实现(尤其是HotSpot)如何在实践中处理SoftReferences? 他们是否“反对清除最近创建或最近使用的软参考”,这是由规范鼓励的?

使用PreparedStatement插入。 如何自动递增ID?

我有一个PreparedStatement,例如: PreparedStatement preparedStatement = connect.prepareStatement(“INSERT into employee (id, time, name” + “(?,?,?)”,Statement.RETURN_GENERATED_KEYS); ResultSet tableKeys = preparedStatement.getGeneratedKeys(); preparedStatement.executeUpdate(); tableKeys.next(); int autoGeneratedID = tableKeys.getInt(1); preparedStatement.setInt(1,autoGeneratedID); preparedStatement.setTimestamp(2, new java.sql.Timestamp(new java.util.Date().getTime())); preparedStatement.setString(3, “Test”); preparedStatement.executeUpdate(); 如您所见,Employee表具有自动递增的ID。 我需要基本上使用preparedStatement自动添加它。 谁能告诉我哪里出错了并纠正我? 现在它只是给我一个与Statement相关的错误。

获取类的所有实例

可能重复: 是否有一种简单的方法可以在Java中获取特定类的所有对象实例 在java中,是否有任何可能的方法来获取某个类的所有实例?

Java URLConnection – 我什么时候需要使用connect()方法?

我有一个问题需要理解URLConnection类中connect()方法的含义。 在下面的代码中,如果我使用connect()方法,如果我不使用它,我会得到相同的结果。 为什么(或何时)需要使用它? URL u = new URL(“http://example.com”); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect();//with or without it I have the same result InputStream in = conn.getInputStream(); int b; while ((b = in.read()) != -1) { System.out.write(b); }

Java – Decimal Format.parse返回具有指定小数位数的double值

我希望能够在格式字符串中给定多个小数位的情况下将字符串转换为Double 。 所以“###,## 0.000”应该给我一个2到3的小数位。 编辑 – 为发生的事情添加了更多信息 用户在UI中输入值 – 输入到String中。 规则是此值限制为3位小数。 底层代码将值存储在数据库中,然后在计算中使用。 因此,尾随小数位将导致计算略微超出预期。 我有以下代码: try { // output current locale we are running under (this happens to be “nl_BE”) System.out.println( “Current Locale is ” + Locale.getDefault().toString() ); // number in Central European Format with a format string specified in UK format String numberCE = “1,234567”; […]