使用 Sun 的专有 Java 类是一种不好的做法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1834826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
It is a bad practice to use Sun's proprietary Java classes?
提问by JARC
The compiler display warnings if you use Sun's proprietary Java classes. I'm of the opinion that it's generally a bad idea to use these classes. I read this somewhere. However, aside from the warnings are there any fundamental reasons why you should not use them?
如果您使用 Sun 的专有 Java 类,编译器会显示警告。我认为使用这些类通常是一个坏主意。我在某处读到过这个。但是,除了警告之外,还有什么根本原因不应该使用它们吗?
回答by dfa
Because they are internal APIs: they are subject to change in a undocumentedor unsupportedway and they are bound to a specific JRE/JDK (Sunin your case), limiting portability of your programs.
因为它们是内部 API:它们可能会以未记录或不受支持的方式更改,并且它们绑定到特定的 JRE/JDK(在您的情况下为Sun),从而限制了程序的可移植性。
Try to avoid uses of such APIs, always prefer a public documented and specified class.
尽量避免使用此类 API,始终更喜欢公共记录和指定的类。
回答by Powerlord
The JDK 6 Documentationincludes a link titled Note About sun.*Packages. This is a document from the Java 1.2 docs, so references to sun.*should be treated as if they said com.sun.*
在JDK 6文档包括标题的链接注意关于sun.*包。这是来自 Java 1.2 docs 的文档,因此sun.*应将引用视为他们说com.sun.*
The most important points from it are:
其中最重要的几点是:
The classes that Sun includes with the Java 2 SDK, Standard Edition, fall into package groups
java.*,javax.*,org.*andsun.*. All but thesun.*packages are a standard part of the Java platform and will be supported into the future. In general, packages such assun.*, that are outside of the Java platform, can be different across OS platforms (Solaris, Windows, Linux, Macintosh, etc.) and can change at any time without notice with SDK versions (1.2, 1.2.1, 1.2.3, etc). Programs that contain direct calls to thesun.*packages are not 100% Pure Java.
Sun公司包括了Java 2 SDK,标准版的类,分为包组
java.*,javax.*,org.*和sun.*。除了sun.*包之外的所有内容都是 Java 平台的标准部分,将来会得到支持。一般而言,sun.*Java 平台之外的软件包(例如 )可能会因操作系统平台(Solaris、Windows、Linux、Macintosh 等)而异,并且可以随时更改,恕不另行通知,因为 SDK 版本(1.2、1.2.1) 、1.2.3 等)。包含对sun.*包的直接调用的程序不是 100% 纯 Java。
and
和
Each company that implements the Java platform will do so in their own private way. The classes in
sun.*are present in the SDK to support the Sun implementation of the Java platform: thesun.*classes are what make the Java platform classes work "under the covers" for the Sun Java 2 SDK. These classes will not in general be present on another vendor's Java platform. If your Java program asks for a class "sun.package.Foo" by name, it may fail with ClassNotFoundError, and you will have lost a major advantage of developing in Java.
每家实现 Java 平台的公司都会以自己的私人方式来实现。中的类
sun.*存在于 SDK 中以支持 Java 平台的 Sun 实现:这些sun.*类使 Java 平台类为 Sun Java 2 SDK “在幕后”工作。这些类通常不会出现在其他供应商的 Java 平台上。如果您的 Java 程序通过名称请求类“sun.package.Foo”,它可能会因 ClassNotFoundError 而失败,并且您将失去使用 Java 开发的主要优势。
回答by Thorbj?rn Ravn Andersen
Try running your code with a non-Sun JVM and see what happens...
尝试使用非 Sun JVM 运行您的代码,看看会发生什么......
(Your code will fail with a ClassNotFound exception)
(您的代码将因 ClassNotFound 异常而失败)
回答by Andreas Dolk
Yes, because nobody guarantees that these classes or API will be the same with the next Java release and I bet it's not guaranteed that those classes are available in Java versions from other vendors.
是的,因为没有人保证这些类或 API 会与下一个 Java 版本相同,而且我敢打赌,不能保证这些类在其他供应商的 Java 版本中可用。
So you couple your code to special Java version and loose at least portability.
所以你将你的代码与特殊的 Java 版本结合起来,至少失去了可移植性。
回答by Tendayi Mawushe
Sun's proprietary Java classes are part of their Java implementation not part of the Java API their use is undocumented and unsupported. Since they are internal they can be changed at any time for any reason that the team working the Sun JVM decides.
Sun 的专有 Java 类是其 Java 实现的一部分,而不是 Java API 的一部分,它们的使用未记录且不受支持。由于它们是内部的,因此可以因使用 Sun JVM 的团队决定的任何原因随时更改。
Also Sun's Java implementation is not the only one out there! Your code would not be able portable to JVMs from other vendors like Oracle/BEA and IBM.
此外,Sun 的 Java 实现并不是唯一的!您的代码将无法移植到来自其他供应商(如 Oracle/BEA 和 IBM)的 JVM。
回答by Jesper
Here is Oracle's answer: Why Developers Should Not Write Programs That Call 'sun' Packages
这是 Oracle 的回答:为什么开发人员不应该编写调用“sun”包的程序
回答by Fabian Steeg
I recently had a case that showed a real-world problem you can hit when you use these classes: we had code that would not compile because a method it was using on a sun.* class simply did not exist in OpenJDK on Ubuntu. So I guess when using these classes you can no longer say things like 'this works with Java 5', because it will only work on a certain Java implementation.
我最近有一个案例,显示了使用这些类时可能会遇到的实际问题:我们的代码无法编译,因为它在 sun.* 上使用的方法在 Ubuntu 上的 OpenJDK 中根本不存在。所以我猜在使用这些类时,你不能再说“这适用于 Java 5”之类的话,因为它只适用于某个 Java 实现。

