使用 Sun 专有的 Java 类是一种不好的做法吗?

发布于 2024-08-12 18:34:53 字数 91 浏览 4 评论 0原文

如果您使用 Sun 专有的 Java 类,编译器会显示警告。我认为使用这些类通常是一个坏主意。我在某处读到过这个。然而,除了警告之外,还有什么根本原因不应该使用它们吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一抹苦笑 2024-08-19 18:34:53

因为它们是内部 API:它们可能会以未记录不受支持的方式进行更改,并且它们绑定到特定的 JRE/JDK (Sun在你的情况下),限制了你的程序的可移植性。

尽量避免使用此类 API,始终首选公共记录和指定的类。

Because they are internal APIs: they are subject to change in a undocumented or unsupported way and they are bound to a specific JRE/JDK (Sun in your case), limiting portability of your programs.

Try to avoid uses of such APIs, always prefer a public documented and specified class.

怀中猫帐中妖 2024-08-19 18:34:53

JDK 6 文档 包含一个标题为 关于 sun.* 包的说明。这是 Java 1.2 文档中的文档,因此对 sun.* 的引用应视为 com.sun.*

其中最重要的一点是:

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。

每个实施Java的公司
平台将自行这样做
私人方式。 sun.* 中的类是
存在于 SDK 中以支持 Sun
Java平台的实现:
sun.* 类使得
Java 平台类工作在“
适用于 Sun Java 2 SDK 的“涵盖”。这些
一般情况下不会出现课程
在另一个供应商的 Java 平台上。如果
你的 Java 程序需要一个类
名称为“sun.package.Foo”,可能会失败
与 ClassNotFoundError ,你会
失去了主要优势
使用 Java 进行开发。

The JDK 6 Documentation includes 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.*

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.* and sun.*. All but the sun.*
packages are a standard part of the
Java platform and will be supported
into the future. In general, packages
such as sun.*, 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 the sun.*
packages are not 100% Pure 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:
the sun.* 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.

初相遇 2024-08-19 18:34:53

尝试使用非 Sun JVM 运行您的代码,看看会发生什么...

(您的代码将因 ClassNotFound 异常而失败)

Try running your code with a non-Sun JVM and see what happens...

(Your code will fail with a ClassNotFound exception)

以为你会在 2024-08-19 18:34:53

是的,因为没有人保证这些类或 API 与下一个 Java 版本相同,而且我敢打赌,也不能保证这些类在其他供应商的 Java 版本中可用。

因此,您将代码耦合到特殊的 Java 版本,并且至少失去了可移植性。

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.

So you couple your code to special Java version and loose at least portability.

审判长 2024-08-19 18:34:53

Sun 的专有 Java 类是其 Java 实现的一部分,而不是 Java API 的一部分,其使用未记录且不受支持。由于它们是内部的,因此可以出于 Sun JVM 团队决定的任何原因随时进行更改。

而且 Sun 的 Java 实现并不是唯一的!您的代码无法移植到 Oracle/BEA 和 IBM 等其他供应商的 JVM。

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.

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.

初懵 2024-08-19 18:34:53

我最近遇到的一个案例展示了使用这些类时可能遇到的现实问题:我们的代码无法编译,因为它在 sun.* 类上使用的方法根本不存在于 Ubuntu 上的 OpenJDK 中。所以我想当使用这些类时你不能再说“这适用于 Java 5”之类的话,因为它只适用于特定的 Java 实现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文