Java应用程序与平台相关的因素有哪些?

huangapple go评论86阅读模式
英文:

What can make Java application platform dependent?

问题

使用Windows路径在应用程序中可能会导致应用程序仅在Windows上运行。

英文:

I am developing an application and wonder what can cause the app to be platform dependent. For example, using Windows path in the app make it app that runs only in Windows.

答案1

得分: 5

以下是翻译的内容:

有很多事情可能导致这种情况:

  • 使用JNI调用本地操作系统特定功能(例如:读写Windows注册表中的值)
  • 依赖于库...它们为您执行此类本机调用

在GUI编程的上下文中,更微妙的版本相同:

  • 依赖于特定的(非标准)字体
  • 依赖于操作系统特定的外观和感觉

正如您在问题中所写:

  • "外部"标识符只以平台特定的方式工作,例如:文件路径

好消息是:所有这些都相当明确。您必须以某种方式编写您的代码以使其与平台相关。

更难的是,但幸运的是不太常见的是编写有效的Java代码...最终依赖于底层JVM的特定实现细节。例如,线程映射到操作系统提供的线程。因此,在不同平台上编写Java代码以表现不同是有可能的。

还请注意,实际上存在各种不同的JVM实现,这在今天的业务世界中很重要。这可能再次导致在运行时出现不同的行为。

长话短说:不要太担心。当您遵循标准的最佳实践(例如:避免完全硬编码的绝对路径名称)时,您的"中等规模"项目不应该存在平台问题。但是一旦涉及到真正大型、复杂的应用程序,情况可能会以许多微妙的方式发生变化。

英文:

There are plenty of things that can get you there:

  • using JNI to make native calls to OS specific features (like: reading/writing values to the windows registry)
  • relying on libraries ... that do make such native calls for you

The more subtle version of the same, especially in the context of GUI programming:

  • relying on specific (non standard) fonts
  • relying on OS specific plafs

As you have written in the question:

  • "external" identifiers that only work in a platform specific way, like: filepaths

The good news here: all of that is rather explicit. You have to write your code in a certain way to become platform dependent.

What is much harder, but luckily less common: writing valid Java code ... that in the end, relies on specific implementation details of the underlying JVM. For example threads are mapped to the threads that your OS provides. So, theoretically, there is a chance to write java code that behaves differently on different platforms.

Also note that there are in fact various different JVM implementations that matter in todays business world. Which can again result in different behavior at runtime.

Long story short: don't worry too much. When you follow standard best practices (for example: by avoiding fully hardcoded absolute path names), then your "medium sized" project should not have platform issues. But as soon as you are talking about really large, complex applications, things can be different, in many subtle ways.

huangapple
  • 本文由 发表于 2020年8月14日 14:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63407765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定