简体   繁体   English

Java跨平台吗?

[英]Is Java cross platform?

I want to develop a cross platform application. 我想开发一个跨平台的应用程序。

Is Java cross platform? Java跨平台吗? I mean, can I develop a Java application in Windows and use it in Mac OS X and Linux? 我的意思是,我可以在Windows中开发Java应用程序并在Mac OS X和Linux中使用它吗?

If yes, how? 如果有,怎么样?

I find application written in Java, has two setup file one for Windows and other for Mac. 我发现用Java编写的应用程序有两个安装文件,一个用于Windows,另一个用于Mac。 This confuses me. 这让我很困惑。

Any illustration or suggestion will be highly appreciated. 任何插图或建议将受到高度赞赏。

Is Java a cross platform? Java是一个跨平台吗?

Java is cross platform in the sense that a compiled Java program runs on all platforms for which there exists a JVM. Java是跨平台的,因为编译的 Java程序在存在JVM的所有平台上运行。 (This holds for all major operating systems, including Windows, Mac OS and Linux.) (适用于所有主要操作系统,包括Windows,Mac OS和Linux。)

I mean I can develop Java application in windows and use it in mac and Linux? 我的意思是我可以在Windows中开发Java应用程序并在mac和Linux中使用它?

Yes, this is possible. 是的,这是可能的。

This (and the security aspect) is one of the main advantages of running the programs in a virtual machine. 这(以及安全方面)是在虚拟机中运行程序的主要优点之一。

If yes how? 如果有,怎么样?

  • Write your application in Java (In .java files) 用Java编写应用程序(在.java文件中)
  • Compile your application using Eclipse or javac (into .class files) 使用Eclipse或javac编译应用程序(转换为.class文件)
  • (Optionally) Bundle your .class files in an executable ( .jar file) (可选)将.class文件捆绑在可执行文件( .jar文件)中

The very same .jar file can be distributed and executed on Windows systems, Mac systems, etc. 可以在Windows系统,Mac系统等上分发和执行完全相同的.jar文件。

I find application written in Java, has two setup file one for windows and other for mac. 我找到用Java编写的应用程序,有两个用于windows的安装文件,另一个用于mac。 This confuses me. 这让我很困惑。

This is because some applications rely on platform-specific features. 这是因为某些应用程序依赖于特定于平台的功能。 They are then bundled with different platform-specific libraries. 然后将它们与不同的平台特定库捆绑在一起。

Unless you're developing an application that itself relies on platform-specific features, (such as for instance low-level system calls), you should be able to do just fine with ordinary Java. 除非您正在开发一个本身依赖于特定于平台的功能的应用程序(例如低级系统调用),否则您应该可以使用普通的Java做得很好。

Important comment by @Peter Lawrey : @Peter Lawrey的 重要评论

It can be while the application is platform independent, the setup program is not. 它可以是应用程序独立于平台的,而安装程序则不是。 eg IntelliJ has three platform specific installers which are not written in Java, but have a zip which you can just unzip on any platform and it will work. 例如,IntelliJ有三个特定于平台的安装程序,这些安装程序不是用Java编写的,但是有一个zip,您可以在任何平台上解压缩它并且它可以工作。

You can run Java applications on every device that has a JVM. 您可以在具有JVM的每个设备上运行Java应用程序。 If it does not, you're out of luck. 如果没有,那你就不走运了。

The comment from Oded is important. Oded的评论非常重要。 C and C++ have compilers on lots of devices. C和C ++在许多设备上都有编译器。

Java byte code won't need to be recompiled when you switch platforms. 切换平台时,不需要重新编译Java字节代码。

C and C++ will require that the developer recompile the application before distributing it to the target system, but once that's done clients will be able to run without an issue. C和C ++将要求开发人员在将应用程序分发到目标系统之前重新编译应用程序,但一旦完成,客户端将能够顺利运行。

The problem of platform-specific customizations and settings has to be dealt with no matter which language you choose. 无论您选择哪种语言,都必须处理特定于平台的自定义和设置问题。 The more your application depends on platform-specific features, the less portable it will be. 您的应用程序越依赖于特定于平台的功能,它的可移植性就越低。

UPDATE: 更新:

Let's revisit the original words in the question: 让我们重温一下问题中的原始单词:

I want to develop a cross platform application. 我想开发一个跨平台的应用程序。

Here's the objective - a direct quote. 这是目标 - 直接引用。 No details about web, mobile, or desktop app. 没有关于网络,移动或桌面应用的详细信息。

Is Java cross platform? Java跨平台吗? I mean, can I develop a Java application in Windows and use it in Mac OS X and Linux? 我的意思是,我可以在Windows中开发Java应用程序并在Mac OS X和Linux中使用它吗?

Define "cross platform". 定义“跨平台”。 Sounds like the bias here is "byte code portability". 听起来像这里的偏见是“字节码可移植性”。 C/C++ can't do that, but "source code portability" is certainly possible as long as you stick to ANSI C/C++ and refrain from using vendor extensions. C / C ++不能这样做,但只要你坚持使用ANSI C / C ++并且不使用供应商扩展,“源代码可移植性”肯定是可能的。

Java's claim to fame from the beginning has always been byte code portability. Java从一开始就声名鹊起,一直是字节码可移植性。 That's what the JVM gets you. 这就是JVM带给你的东西。 That does not mean your whole application will be portable, because you might not have managed other dependencies well. 这并不意味着您的整个应用程序都是可移植的,因为您可能没有很好地管理其他依赖项。

If I substitute "C/C++" for "Java in that bloc, then cross platform means something different. I cannot pick up a .exe or .so compiled for one platform and expect to run it on another, but if I create an .exe or .so for each platform and make them available it's certainly possible to make the same source code runnable on multiple platforms. 如果我用“C / C ++”代替“那个集团中的Java,那么跨平台意味着不同的东西。我不能为一个平台选择.exe或.so编译,并希望在另一个平台上运行它,但如果我创建一个。 exe或.so对于每个平台并使它们可用它当然可以使相同的源代码在多个平台上可运行。

If yes, how? 如果有,怎么样?

If you have packaged your Java app as a JAR, you can put that on any platform you like. 如果您已将Java应用程序打包为JAR,则可以将其放在任何您喜欢的平台上。

If you have multiple C/C++ .exes for the platforms you're interested in, you can certainly run it when you need to. 如果您对您感兴趣的平台有多个C / C ++ .exes,那么您当然可以在需要时运行它。

There is an important caveat with regard to Java portability. 关于Java可移植性有一个重要的警告。 "Business logic" (non-UI stuff) is quite portable, but there are at least a half-dozen different (and incompatible) user interface paradigms for Java, so, eg, Java code written to run on an Android (even ignoring Android's incompatible JVM) won't run on a Nokia phone, and code for either one will not run on a desktop PC. “业务逻辑”(非UI东西)非常便携,但是至少有六种不同(和不兼容)的用户界面范例适用于Java,因此,例如,编写为在Android上运行的Java代码(甚至忽略了Android的不兼容的JVM)不能在诺基亚手机上运行,​​任何一个的代码都不能在台式机上运行。

But there's no other language that does better, to my knowledge. 但据我所知,没有其他语言可以做得更好。

I wrote a little game in Java with sound. 我用Java写了一个带声音的小游戏。 I've used standard library (AudioInputStream and Clip classes). 我使用过标准库(AudioInputStream和Clip类)。 The program works very good with Windows XP, BUT I haven't got the sounds with Windows Seven or Linux Ubuntu. 该程序在Windows XP中运行良好,但我没有得到Windows 7或Linux Ubuntu的声音。

Java 6 was installed for all those OS, and I've compiled the program with the standard compiler JAVAC. 为所有这些操作系统安装了Java 6,我使用标准编译器JAVAC编译了该程序。

So Java is not so "cross platform". 所以Java不是那么“跨平台”。

JAVA is not really cross-platform because is just a design, some JAVA implementations can be cross-platform but it really depends on the JAVA implementer. JAVA并不是真正的跨平台,因为它只是一种设计,一些JAVA实现可以是跨平台的,但它实际上取决于JAVA实现者。

SUN/Oracle makes the JAVA standard implementation for Windows, MAC and Linux and if you build programs with their JDK you should run the programs with their JRE, if you try to run it with other JRE there might be compatibility issues. SUN / Oracle为Windows,MAC和Linux制定JAVA标准实现,如果使用JDK构建程序,则应使用其JRE运行程序,如果尝试与其他JRE一起运行,则可能存在兼容性问题。

This can be seen more in the mobile, some phone makers won't make a full implementation of JAVA because hardware limitations or because they want to control the device's software, they carry the JAVA logo but you must use their own JDK and those programs could not run outside of their JVM implementation. 这在移动设备中可以看得更多,一些手机制造商不会完全实现JAVA,因为硬件限制或者因为他们想要控制设备的软件,他们带有JAVA标识,但你必须使用他们自己的JDK,那些程序可以不要在JVM实现之外运行。

Another issue with JAVA is that some functionality could be deprecated and what ran in some specific version of the JRE could not run in newer versions or run in a different way. JAVA的另一个问题是某些功能可能会被弃用,而某些特定版本的JRE中运行的功能无法在较新版本中运行或以不同方式运行。

The big problem is when we are talking about mobile apps. 最大的问题是当我们谈论移动应用程序时。 For desktops OS, java can run perfectly, but for mobile devices, developers can, practically, only create for Android. 对于桌面操作系统,java可以完美运行,但对于移动设备,开发人员实际上只能为Android创建。 There are tools like Totalcross that helps java developers to create apps to iOS and Android. Totalcross等工具可以帮助Java开发人员为iOS和Android创建应用程序。 The focus is maintain the WORA assumption to every platform, even mobile. 重点是将WORA假设保持在每个平台上,甚至是移动平台。

Yes, Java written and compiled on one OS can run on another OS. 是的,在一个OS上编写和编译的Java可以在另一个OS上运行。 There are JVMs available for many modern operating systems. JVM可用于许多现代操作系统。

Java apps are packaged as .jar files. Java应用程序打包为.jar文件。 These can run on any operating system that implements the correct Java Runtime Environment for the application, provided the user has installed that JRE (JREs are provided to users for free). 这些可以在为应用程序实现正确Java Runtime Environment的任何操作系统上运行,前提是用户已安装该JRE(JRE免费提供给用户)。

The precise procedure for running the app is different for each operating system. 对于每个操作系统,运行应用程序的精确过程是不同的。

The Java Development Kit provides libraries that allow system functions (eg file operations, user interfaces) to be invoked that ought to work on any OS. Java Development Kit提供了一些库,允许调用应该在任何操作系统上运行的系统函数(例如文件操作,用户界面)。 However the precise behavior may vary (in a similar way to how a website can look different in different browsers). 但是,精确的行为可能会有所不同(与网站在不同浏览器中的外观类似)。 Also, Java apps may have a 'look and feel' that does not precisely match the look and feel of applications designed specifically for that OS. 此外,Java应用程序可能具有“外观和感觉”,与专门为该操作系统设计的应用程序的外观和感觉不完全匹配。

It depends what you mean by "cross-platform". 这取决于你的“跨平台”的意思。 Java code can be compiled to byte-code binary class or jar files on any OS supporting the JDK and run on any platform supported the JRE. Java代码可以编译为支持JDK的任何操作系统上的字节码二进制类或jar文件,并在支持JRE的任何平台上运行。 However, most compiled languages can be compiled on any platform provided the designers has catered for it. 但是,大多数编译语言都可以在任何平台上编译,前提是设计人员可以满足它。 Java is more easily portable than, say, C++, but that doesn't mean C++ code won't be able to compile on any platform. Java比C ++更容易移植,但这并不意味着C ++代码无法在任何平台上编译。

In other words - don't choose your language on this single feature alone. 换句话说 - 不要单独选择这个单一功能的语言。 Choosing the best language for your application would be a better way of going about it. 为您的应用选择最佳语言将是一种更好的方式。 Perhaps a scripting language would be best for you. 也许脚本语言最适合您。 Maybe post another question asking what language to use to solve your specific problem. 也许发布另一个问题,询问使用什么语言来解决您的具体问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM