Is Java cross platform? - java

I want to develop a cross platform application.
Is Java cross platform? I mean, can I develop a Java application in Windows and use it in Mac OS X and Linux?
If yes, how?
I find application written in Java, has two setup file one for Windows and other for Mac. This confuses me.
Any illustration or suggestion will be highly appreciated.

Is Java a cross platform?
Java is cross platform in the sense that a compiled Java program runs on all platforms for which there exists a JVM. (This holds for all major operating systems, including Windows, Mac OS and Linux.)
I mean I can develop Java application in windows and use it in mac and 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)
Compile your application using Eclipse or javac (into .class files)
(Optionally) Bundle your .class files in an executable (.jar file)
The very same .jar file can be distributed and executed on Windows systems, Mac systems, etc.
I find application written in Java, has two setup file one for windows and other for 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.
Important comment by #Peter Lawrey:
It can be while the application is platform independent, the setup program is not. e.g. 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.

You can run Java applications on every device that has a JVM. If it does not, you're out of luck.
The comment from Oded is important. C and C++ have compilers on lots of devices.
Java byte code won't need to be recompiled when you switch platforms.
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.
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? I mean, can I
develop a Java application in Windows
and use it in Mac OS X and 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.
Java's claim to fame from the beginning has always been byte code portability. That's what the JVM gets you. 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.
If yes, how?
If you have packaged your Java app as a JAR, you can put that on any platform you like.
If you have multiple C/C++ .exes for the platforms you're interested in, you can certainly run it when you need to.

There is an important caveat with regard to Java portability. "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.
But there's no other language that does better, to my knowledge.

Yes, Java written and compiled on one OS can run on another OS. There are JVMs available for many modern operating systems.

Java apps are packaged as .jar files. 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).
The precise procedure for running the app is different for each operating system.
The Java Development Kit provides libraries that allow system functions (e.g. file operations, user interfaces) to be invoked that ought to work on any OS. 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.

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. 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.
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.

I wrote a little game in Java with sound.
I've used standard library (AudioInputStream and Clip classes).
The program works very good with Windows XP, BUT I haven't got the sounds with Windows Seven or Linux Ubuntu.
Java 6 was installed for all those OS, and I've compiled the program with the standard compiler JAVAC.
So Java is not so "cross platform".

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.
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.
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.
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.

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. There are tools like Totalcross that helps java developers to create apps to iOS and Android. The focus is maintain the WORA assumption to every platform, even mobile.

Related

How to make Java Mac App that doesn't require installing Java

I want to make a Mac Application using Java code I wrote, and I want to make it that the person using it doesn't require Java to be installed on their own computer.
Is there a way to create a Mac Application so it can run on any mac, whether they have Java installed or not?
Either bundle a JVM or compile native
You have a choice of three ways to deliver a local Java-based app to a Mac user.
User installs a JDK or JRE on their machine. (Or I.T. sysadmin person does so.)
App comes bundled with a JVM for a specific OS and specific chip architecture.
App is compiled to native code for a specific OS and specific chip architecture.
Install JDK/JRE
In a controlled environment such as a corporation or school, it may be practical to install a JDK or JRE on every Mac. If you are writing JavaFX apps, you could install an edition of a JDK/JRE that comes with the OpenJFX libraries.
Your Java app can then execute by using the already-installed JVM.
By the way, in such a controlled environment, OpenWebStart is a way to deliver and launch a local Java app by using the convenience of a web browser.
In contrast, Oracle has abandoned the approach of expecting common consumer machines to come equipped with Java pre-installed. For details, see the Oracle white paper, Java Client Roadmap Update. So expecting individuals to have Java installed is not practical.
Bundle JDK/JRE
You can build your app in such a way as to include a JDK/JRE for a specific kind of machine, meaning a specific operating system and a specific chip architecture.
For Macs, that means you need one build for Macs with Intel (x86) chips, and another build for Macs with Apple Silicon (ARM, AArch64) chips. You would need to to either supply two separate versions of your app, one for each child, or perhaps a “fat binary” that includes two JDKs/JREs for both chips. The fat binary approach was supported by Apple (and its originator, NeXT Inc.) in previous chip transitions, but I’ve not verified is this is the case now in the Intel to Apple Silicon transition.
Modern tooling helps with embedding a JDK/JRE. See jlink and jpackage. See Packaging Tool User's Guide. As of Java 9, the Java Platform Module System enables including only the parts of the JDK/JRE actually used by your particular app. Unused parts are omitted, making for a smaller-sized final app artifact.
Compile native
The last way uses GraalVM technology to compile your code to native code specific to the targeted runtime machine. Instead of compiling to bytecode for a conventional JVM to interpret and optionally JIT-compile at runtime, full compilation to native machine language is performed when you build your app. This approach is cutting-edge, perhaps bleeding-edge. So research carefully and test thoroughly if you dare to consider this novel approach.
With both of the last two approaches, you have the option of distributing through the Apple App Store.
You don't need JDK to install on the client machine but as we know java converts the program into bytecode and a java compiler is needed to compile that bytecode into machine language you must install JRE on the client computer whether it is mac or windows or any other operating system

If I install a new library, will my application still be portable?

I am writing a macro scripting program in Java (JavaFX). I would like to add global keyboard listeners to interact with the program while it is not focused. I have heard of JNativeHook, and it looks useful. My question is, if I decide to use this library, will my code still be portable? Could I still distribute it to others without requiring them to install other things?
You can absolutely include native libraries in a portable app as long as your code can find the DLLs in question and you bundle them with your software. I personally use SIGAR in portable apps and it works fine as long as the relative path remains the same, depending on how you load your DLLs.
******EDIT******
That is of course if you remain within a Windows environment and you are indeed talking about DLL files. If you mean additional JAR files that do not require anything else than java code contained withing JAR files then yes your app will be fully portable, cross-platform too. That is what i love about Java, i once developed a large app on Windows and it ported to Ubuntu with no modifications whatsoever.
You can make your app portable if you don't need any special insulation for a different device uses different platform so in your case the library you asking for JNativeHook need some requirements mentioned here https://github.com/kwhat/jnativehook#software-and-hardware-requirements. You have to give instruction to your user to have these requirements based on the platform he/she works on.

Is there any possibility to run an Android Application on IOS7,6,5..?or wise versa

i want to make an app in Java
my question is does it possible that Program Written in Java could run on both Platform Android and ios
if yes then What to do any small Tips...
because Java is Platform independent.
No. While Java may be "Platform independent" that does not mean it is supported on all platforms. It is not supported on iOS.
No, java use in Android whereas objective-c use in ios.
So, it is not possible.
If you want to run same code on both platform then go hybrid application using HTML5, CSS3 and javascript or jQuery.
Platform independent doesnt the meaning stated by zaph, It means you can run the same compiled code in what ever operating system which has java. Eg: you can run a java program compiled in windows and copy those class files and you can run it on a linux which has the jre. (While c and c++ are platform dependent)
You cant run android code in IOS is simply because they dont have java compilers. C and C++ are basic languages used to write operating system, Even java VM is wriiten in c++. Check this
If you want to run an app on both android and IOS . you should go for PhoneGap Now a days. We have alternating options like hybrid apps which is already suggested.
Running Java on iOS is not supported because of restrictions imposed on the iOS Developer Program License. Though Java is platform independent, an application built using Java, needs a Java Virtual Machine (JVM) to execute bytecode which your Java application ultimately compiles to. Since iOS does not support a JVM, it will not be possible to run Java on an Apple device.
Applications that run on multiple platforms usually run as web views, implying that they are based on HTML. A popular tool in use is PhoneGap or Cordova. You could use these to develop platform-independent applications.

Can Android NDK include a windows dll to a an Android shared library?

Is it even possible to link/include a dll file in the ndk-build command and produce a shared library that can access dll functions from android java code.
I have been trying to do it, I tried to use SWIG to create a java wrapper code to dll and I didn't succeed, then I started to think is it even possible!
Thanks.
No. A Windows DLL is in a different format to Android and Linux.
Windows uses a format known as PE COFF: "Portable Executable, Common Object File Format". Although COFF itself comes from UNIX, Microsoft has its own version. Further incompatibilities exist in order to support .Net objects. Most UNIX's and Linux use ELF - Execute and Link Format.
Even if they were, by some miracle, using the same format, it probably still would not work, because the machine architectures would have to be the same, e.g. ARM (and there are several configurations of ARM) or Intel x86.
And there's more. Even if you managed to get the DLL to run, what else does it use? DLLs often have other external dependencies, for example the C runtime library, MSVCRT, or kernel.dll.
So, get the source code and recompile it natively? That might work, but Windows DLLs sometimes have an optional DLLMain() entry point which Linux .so files have no direct equivalent. What about the rest of the code? Is it written in a portable manner? Has it stuck to using the ISO language standard throughout? To be truly portable, code has to be designed and written that way.
No. Not without building a whole Windows compatibility layer. Android is not compatible with Windows out of the box.
Also, Windows DLLs tend to be compiled for Intel x86 CPUs, while the majority of Android devices have ARM CPUs. While Windows for ARM exists (Windows Phone, Windows RT to name some), the environment that the DLL is supposed to live in doesn't exist on Android.

Windows DLL on Linux System

I wonder if you can load a DLL, of Windows operating system (for example, user32.dll), in an application (in any programming language) with a Linux operating system.
The DLL would be in a directory on the Linux file system.
Thanks for the help.
Greetings!
Yes, you can load a DLL and call its functions by using an appropriate wrapper library, but that's fundamentally useless if the DLL itself has dependencies on the platform which are not present. This approach is used for things like closed-source video codecs, where no (nontrivial) dependencies exist.
User32.dll is of course, part of the OS and intrinsically linked to many NT kernel functions, none of which exist in Linux. Wine does not make use of the Windows user32.dll, but provides its own version which gives equivalent functionality.
Loading Windows' user32.dll would definitely not be useful, as it is mostly going to be a wrapper for other DLLs, processes (for example CSRSS) and kernel calls which aren't present under Linux. You could load and use Wine's one though.
No. The architectures are fundamentally different.
I note your question is tagged C++. If it was a .net DLL (built with CLR bytecode) then you could reference it via an application running under Mono.
It's possible if you write a wrapper for it. That's how the win32 codecs work on Linux. Also Wine uses many DLLs.
You could probably do a custom Wine build (home page, Wikipedia page) for your application. We're talking about a big hammer here, though. :-)
DLL is a format for windows (i.e. it MS implementation of shared library concept).
It is not in a format that Linux understands.
Linux uses the ELF format for dynamic libraries.
Why do you want to do this? To execute a program (.exe) that uses that DLL? In that case you need a Windows emulator on your Linux machine, such as wine.

Categories

Resources