Using Java 7 SDK features in Java 6

為{幸葍}努か 提交于 2019-11-29 09:53:15

Just build with -target 1.6 and organize your code so you can catch the ClassNotFoundExceptions and NoClassDefFoundErrors cleanly around the modules that use 1.7. Maybe load them with a separate class-loader for example.

You can build for java 1.6 easily as toolkit has pointed out. However, you need to make sure that you don't accidentally access any methods which don't exist in java 6. This will cause a runtime exception in your production code.

If you're using maven, you can use the maven-enforcer-plugin which makes sure that no java 1.7 classes or method calls sneak into your code built for 1.6.

An example would be the change from java 1.4 to 1.5. I was building with 1.5 with a target of 1.4, and I accidentally used:

new BigDecimal(5);

This compiled fine, and ran fine for me. But because the client was still using 1.4, it failed. Because this constructor doesn't exist in 1.4. It was introduced in 1.5.

Another solution would be to build a couple of jars, one with the new nio stuff, one with the old stuff, and detect at installation time whether or not the user was running java 1.7. If so, add the jar that contains the appropriate implementation.

For some elements that were added in Java 7, you might be able to find Java 6 jsr jars that give you the functionality. I do not believe this will be the case for the File System Watcher however.

In terms of file system watchers, before Java 7 I used to just poll a file's properties every few seconds or so to check it hadn't changed. It's not exactly nice, but practically it uses no noticeable resources and from an end user's perspective appears to work the same.

If you're after a more comprehensive library, check out http://commons.apache.org/jci/commons-jci-fam/index.html - I believe that does something similar, though I've never used it.

Specifying source 1.7 and target 1.6 I'm pretty sure won't work, I tried it for a different reason a while back and from memory the JVM complained about incompatible flags (my guess is because of the new invokedynamic in 7.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!