我有兴趣使用Java 7 SDK中的一些NIO2功能(特别是文件系统观察者),但是我不想为Java 7编译我的类并排除Java 6运行时.主要是因为我希望保持与Mac OS X的兼容性,还因为我不想强迫我的用户升级.
这可能吗?最好的方法是什么?任何链接或示例?
以下是我可以想象的一些方法:使用不同的编译器编译类文件并根据Java版本动态加载它?或者也许使用反射?或者也许只有Java 7的编译器设置来生成Java 6兼容的类?
我正在寻找一个不会变成丑陋混乱的解决方案:),所以理想情况下我可以编写两个接口实现,一个使用新功能,一个不使用,然后动态选择一个而不必进行反射调用到处都是.
我做了一个简单的Swing应用程序.但渲染表现出错误.我做错了什么或者它是一个错误?
这是一个简单的小型JFrame,带有文本字段,按钮和空列表.如果我首先水平调整窗口大小然后键入文本字段,按钮会突然消失.
这是我的代码:
public class App extends JFrame {
public App() {
JTextField messageFld = new JTextField();
JButton saveBtn = new JButton("Save");
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(messageFld, BorderLayout.CENTER);
inputPanel.add(saveBtn, BorderLayout.EAST);
JList<Data> list = new JList<Data>();
JPanel panel = new JPanel(new BorderLayout());
panel.add(inputPanel, BorderLayout.NORTH);
panel.add(list, BorderLayout.CENTER);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Test application");
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new App();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
以下是一些截图:
在启动时

水平调整大小后

在文本字段中列出了几个字符

将鼠标移到按钮上后

我使用Windows 7,Java …
Java 方法类和Java 7的MethodHandle类都引用与方法关联的对象,但它们很少使用,当函数需要传递给另一个时,最好使用实现包含一个的接口的匿名类方法.
(注意:MethodHandles应该比旧方法更快.)
为什么这些结构不经常用于将函数传递给函数?是因为它们仍然冗长吗?
代码示例:
public final class HigherOrder {
public static final List<?> map(final List<?> list, final MethodHandle mh) throws Throwable {
if (list == null) return null;
List<Object> ret = new ArrayList<>(list.size());
for (Object element : list) {
ret.add(mh.invoke(element));
}
return ret;
}
public static final Object reduce(final List<?> list, final MethodHandle mh) throws Throwable {
if (list == null) return null;
Object tmp = list.get(0);
for (int i = 1; i < list.size(); …Run Code Online (Sandbox Code Playgroud) 我有与升级Eclipse java编译器相反的问题:我正在使用可以支持JDK 1.7的Eclipse 3.7.1 ,但是现在我正在为JDK 1.6开发...它一直工作正常,但突然间我得到了吨当我做一个蚂蚁构建时,这样的错误(见下文).
我想使用JDK 1.6; 我在哪里设置?Eclipse设置正确(每个项目的Java Build Path指向JRE系统库[jdk1.6.0_16]),但是ant似乎忽略了它.
[javac] warning: java\nio\ByteBuffer.class(java\nio:ByteBuffer.class): major version 51 is newer than 50, the highest major version supported by this compiler.
[javac] It is recommended that the compiler be upgraded.
[javac] warning: java\lang\Object.class(java\lang:Object.class): major version 51 is newer than 50, the highest major version supported by this compiler.
[javac] It is recommended that the compiler be upgraded.
[javac] warning: java\util\Collections.class(java\util:Collections.class): major version 51 is newer than 50, the …Run Code Online (Sandbox Code Playgroud) 这是关于线程优先级的测试.代码来自Thinking in Java p.809
import java.util.concurrent.*;
public class SimplePriorities implements Runnable {
private int countDown = 5;
private volatile double d; // No optimization
private int priority;
public SimplePriorities(int priority) {
this.priority = priority;
}
public String toString() {
return Thread.currentThread() + ": " + countDown;
}
public void run() {
Thread.currentThread().setPriority(priority);
while (true) {
// An expensive, interruptable operation:
for (int i = 1; i < 10000000; i++) {
d += (Math.PI + Math.E) / (double) i;
if (i …Run Code Online (Sandbox Code Playgroud) 我们有一个通过使用SWT库的webstart启动的应用程序.
众所周知,osx需要swt库在主线程中运行,所以我们在jnlp中有这个:
<resources os="Mac">
<j2se version="1.6+" java-vm-args="-XstartOnFirstThread"/>
<jar href="swt-mac64.jar"/>
</resources>
Run Code Online (Sandbox Code Playgroud)
由于我们已经升级到Java 7(1.7.0_10),似乎webstart没有将"-XstartOnFirstThread"传递给它启动的java进程.
我可以从'ps'看到java进程没有得到"-XstartOnFirstThread"选项.
有谁知道如何让webstart回到之前的行为?
问候,戈德.
这是webstart日志显示的内容:
Java Web Start 10.10.2.18
Using JRE version 1.7.0_10-b18 Java HotSpot(TM) 64-Bit Server VM
User home directory = /Users/shier
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
m: print memory usage
o: trigger logging
p: reload proxy configuration
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread …Run Code Online (Sandbox Code Playgroud) 这似乎是一个疯狂的错误,但Enum.valueOf(type, name)在Oracle JDK 7 SE上似乎不稳定.
表现形式是,在完全相同的名称字符串(我已经验证了这一点),调用valueOf()有时会抛出IllegalArgumentException消息No enum constant ....
我的团队通过Eclipse调试器运行了这个,我们注意到的是,在enum上的valueOf的以下JDK实现中enumConstantDirectory(),即enum 的values()列表,有时似乎缺少某些值.不是枚举本身定义的所有值的全部.
我可以通过Enum.valueOf(enumclass.class, "XXX")在JVM启动时调用所有可能的枚举值来解决这个问题.当我这样做时,似乎values()总是包含全套.
但是,如果我不做这种类型的初始化,有时Enum.valueOf()会抛出一个IllegalArgumentException.
上下文:我在使用XStream 1.4.4转换转换枚举的POJO对象时遇到了这个问题,但问题似乎并不是XStream本身.
有没有人见过这种错误?如果你有,我很想听听.这令我难以置信.这是Oracle JDK/JVM实现中的错误吗?
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
String name) {
T result = enumType.enumConstantDirectory().get(name);
if (result != null)
return result;
if (name == null)
throw new NullPointerException("Name is null");
throw new IllegalArgumentException(
"No enum constant " + enumType.getCanonicalName() + "." + …Run Code Online (Sandbox Code Playgroud) Java 7 Update 25今天刚刚发布,导致我公司开发的框架的某些功能失败.在过去一年中,大多数Java更新都在发生这种情况.如果甲骨文有某种开发者协议允许公司在发布之前测试新更新,那么公司就有时间准备解决任何问题并告知客户做好准备,这将是一件好事.
如果我事先知道这些问题,我就不会更新到Java 7 Update 25,并且会告知我的客户等到创建补丁以进行更新.(告诉他们不更新比让我们的支持部门解释如何访问Oracle JAVA存档并创建免费的Oracle登录以访问安装程序更容易.)
所以我的问题是:有人知道在正式发布之前收到新Java更新副本的方法吗?
我正在尝试从URL打开一个文件.
URL的对象是使用ClassLoader的getResource()方法创建的.从getResource()方法返回的输出URL是=
file:/C:/users/
Run Code Online (Sandbox Code Playgroud)
使用URL.getFile()方法返回String为"/ C:/ users /"后,它删除" file: "而不是" / "这个/在使用新的FileInputStream打开文件时出错. 错误:FileNotFoundException
文件名起始处的"/"导致获取路径对象时出现同样的问题.这里,从URL.getResource().getFile()中检索目录的值.
Path Dest = Paths.get(目录);
收到的错误是: java.nio.file.InvalidPathException:索引2处的非法char <:>:/ C:/ Users /
有人面对这样的问题吗?
我正在尝试使用java程序连接到smartsheet api.最初我遇到了站点证书的问题,该证书通过将其添加到java密钥库来解决.现在,当我尝试运行我的代码时,我收到以下错误.
Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at com.smartsheet.api.internal.http.DefaultHttpClient.<init>(DefaultHttpClient.java:64)
at com.smartsheet.api.SmartsheetBuilder.build(SmartsheetBuilder.java:203)
at SmartsheetConnection.main(SmartsheetConnection.java:13)
Run Code Online (Sandbox Code Playgroud)
这是我的代码(我按照他们的文档).
import com.smartsheet.api.*;
import com.smartsheet.api.models.*;
import com.smartsheet.api.models.enums.*;
import com.smartsheet.api.oauth.*;
public class SmartsheetConnection {
public static void main(String[] args) throws SmartsheetException {
// Set the access token.
Token token = new Token();
token.setAccessToken("foo");
Smartsheet smartsheet = new SmartsheetBuilder().setAccessToken(token.getAccessToken()).build();
}
}
Run Code Online (Sandbox Code Playgroud)
投掷错误的行是(第144行)
@Deprecated
public static final X509HostnameVerifier ALLOW_ALL_HOSTNAME_VERIFIER
= AllowAllHostnameVerifier.INSTANCE;
Run Code Online (Sandbox Code Playgroud)
但我不知道该怎么做.我正在使用maven来获取依赖项.它与Apache HttpComponents的版本有关吗?
这是pom.xml
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target> …Run Code Online (Sandbox Code Playgroud)