http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u3-download-1501626.html
We know that java is a Platform Independent Language then why does this site provide JDKs for all OSes like Linux, Windows, Solaris?
Then why do we tell java is Platform Independent?
it is like this:
your application
---------------------
JAVA on OS1
---------------------
OS1
---------------------
hardware
---------------------
if you write your application on top of Java, then you can just move your java
application, as is, without changing it, or even compile it, to new OS, because
your program is written on one platform, which is Java, not the native OS.
So, you need to download specific Java for your OS. But from application point of view, it is the same API. Java makes your application platform independent since it hides the OS from your application. But Java itself, it has to be compiled and build for each specific OS. But the application do not care about that. The application sees the same API. That is the whole point.
Because there you download the Installer for the Java Virtual Machine. This is the environment where your Java Application is running in.
The reason why Java is OS independent, is because its running in this JVM.
The JVM's job is to hide the differences between platforms and to provide the same execution environment to application code regardless of platform.
The JVM is written in C++, and is compiled to a native binary, just like any other C++ application. (You wouldn't expect a .exe file to run on Linux, after all).
So the JVM is platform-specific, but the environment it provides is not.
To explain you in simple terms, you no need to compile your java source code when you move your code from one OS to another, but to run your compiled java code you need to have OS specific Java run time machine. Thats why you have different JDK for different OS.
To add to others answers, Java is qualified Platform independent because the code you write is supposed to works on every platform. That's not totally true in fact. The Java code is always compiled in bytecode in the same way, but the JVM interprets this common bytecode in different way in function of the OS, there is one JVM per OS. OS that don't have a JVM implementation to use bytecode can't support Java.
Related
I found some reference that says I can add #!/usr/bin/java --source 12 at the beginning of the file and run the file directly from the terminal.
I am able to run using this on my local machine, however, when I tried the same on Github action I'm getting the error error: invalid value for --source option: 12
I'm not really an expert in shell scripting or java, can someone help me understand what this --source means is it the java version, I tried setting up the same version (jdk18) on Github action but still did not work.
java (the runtime executable) can only run class files. Until, that is, java12, where a normal JDK distribution (and not the bizarro JREs that some packagers like Azul publish ^1) has a java.exe that can run java files straight up. It's simple sugar - the compiler is still involved, of course. It's just that java will execute it for you.
You don't need --source 12; just java MySourceFile.java is fine. Edit that comment at the top of your source file, it should just be #!/usr/bin/java. The thing you do need is that java on your command line PATH is a java v12 or higher, and it is not. There isn't anything your java source file can do about this, you're going to have to impose on your users to have at least java12 installed or this simply does not and can not be made to work. It looks like you're on some linux distro or other; apt or yum or snap or whatever package manager you have will tell you how to fix this: Install java17, and uninstall the rest or use java-alternatives or whatever mechanism your package manager has to set the default executable (the one /usr/bin/java links to). Read the documentation of the package supplying java17, possibly following some links to the generalized java infrastructure package, it should tell you.
Mostly this is a red herring, this just isn't how java files are distributed. It's virtually pointless because:
Java is not a language that tends to be used for quick shell script-esque things. Such things tend to be self-fulfilling prophecies: Because nobody does this, library authors aren't thinking about it when they develop their APIs and the users of their libraries won't file enhancement requests for this either. Because common libraries aren't convenient when used for quick off-the-cuff shell scripts, java isn't used for it, thus perpetuating the cycle.
Any serious java app would definitely involve packages, dependencies, and more - and such apps cannot be run like this.
Class files are as platform agnostic as source files are. There is no sane reason to distribute java-written shell-script-esque tooling as a source file instead of a jar, except for off-the-cuff editing off them, which gets you right back to point #1 and #4.
The java core APIs work on a model of lowest common denominator: If there is a major OS that cannot or doesn't work in a certain way, then java simply does not expose this at all. For example, on all posix systems (i.e. pretty much every major OS except windows), you have your usual TERM, KILL, HUP, etc signals. Java core libs don't let you interact with them (unless you dip into hidden sun.misc.* API which doesn't reliably work in the first place). This makes java extra unsuitable for quick command line scripting where you want a different model: If at least one OS can do it, the language should have a library for it, and that library should simply fast-crash if you attempt to use it on an OS that doesn't support it. One easy way around this is a third party library that adds support for OS-specific stuff, but your model of distribution (stick #!/usr/bin/java at the top and distribute a source file) cannot include dependencies.
Java as a runtime model is mostly focused on running things eventually very quickly, at the cost of starting off slowly. This is fantastic for web servers which need to run efficiently but will be running for quite some time. It's utterly unsuitable for shell scripting, though.
CONCLUSION: You don't want to stick #!/usr/bin/java at the top even if you could make it work.
[1] A JRE is a java distribution without compilers and other development tools like jstack. These cannot run java SomeSourceFile.java, obviously; they do not have a compiler. However, JREs died - there are no JREs anymore; JDK8 is the last one that shipped with an official JRE. The JRE serves as a distribution model: The end user installs a JRE, and you ship your jars to them. This model is obsolete (you are now responsible to get something that can run your class files on the deployment machine), and therefore JREs died. However, some packagers of OpenJDK builds, such as Azul, still publish them, confusing matters. Hence, 'bizarro'. Azul and co have relatively good reasons for doing it, but, you shouldn't be using these unless you really know what you are doing.
I am not able to understand that after module system is introduced in our java language. Is java9 and above still platform independent or not ? I am asking this question because I have read that now every application will have its own jre inside it. So, how will this single jre run on all OS, like windows, Linux, or Mac OS.
You are conflating two different changes recently made to the Java platform:
Retiring of Java Web Start & Applet technologies
Modularization
Retiring desktop-technologies
Recently Oracle announced the phasing out of the Java Web Start technologies, in addition to the already-deprecated Applet technology. See item JDK-8184998 in Java 9 Release Notes:
Java Deployment Technologies are deprecated and will be removed in a future release
Java Applet and WebStart functionality, including the Applet API, The Java plug-in, the Java Applet Viewer, JNLP and Java Web Start including the javaws tool are all deprecated in JDK 9 and will be removed in a future release.
End-users will no longer be encouraged to install a JDK or JRE on their computer.
For more details, see the eight-page 2018-03 white paper from Oracle, Java Client Roadmap Update.
So then, how are developers of Swing or JavaFX apps to deliver their software to the end-user?
Oracle suggests packaging up your app along with a JVM & JRE for delivery as a single launch-ready applications on that appears on the client to be just another app alongside the native apps. Such “double-clickable” app-packaging has been commonly done on the Mac since the beginning of Java. But what was once an obscure art on other host environments (Linux, BSD, Windows, etc.) will now be the norm, as it is on macOS.
In yesteryear, bundling a Java runtime with your app required jumping over some licensing hurdles. The legalities have eased with arrival of the open-source OpenJDK project, and possibly with other implementations†.
You will need to prepare different releases of your app for each hosting environment. While your Java code runs independently of the host OS, the JVM is built of native code to interact with one specific kind of host. So you will need to build a Linux release with a Linux JVM, a macOS release with a macOS JVM, and so on. While that may seem like a downer, the upside is that you no longer need to worry about users having the wrong JVM version installed, or no JVM at all. Now the JVM’s presence and version are under your control. Your end-users and customers will no longer need to be aware that your app is Java-based.
Modularization
That need for app-packaging has nothing to do with the modularization of Java. As I said, it has been done for decades on the Mac.
What modularization brings to the party is that the JVM/JRE you bundle into your delivered app can be customized to contain only the Java Modules actually utilized by your particular app. This results in a smaller size, so your finished app is smaller, downloads are faster, less storage is used, and your app may load faster.
The open-source jlink “Java Linker” tool helps with the packaging work, so you can assemble and optimize a set of modules and their dependencies (only the ones actually called by your app) into a custom run-time image. This modular run-time image format is defined in JEP 220.
† On a related note, you may want to read the white paper Java Is Still Free to understand how and where to obtain a Java implementation for your app, and what support may or may not be offered in either free-of-cost or paid releases.
By the way, you may find helpful this Answer on a related Question, with a flowchart of choosing various sources of a Java implementation.
Is java9 and above still platform independent or not ?
Yes. It's as platform independent as it ever was. The module system has nothing to do with platform independence.
now every application will have its own jre inside it.
It doesn't have to, but it's more and more recommended as time goes on since fewer people have Java installed separately on their systems. This used to be a given, but that number has been declining for the last decade or so, and now (outside of Java developers) pretty much no-one has a standalone JRE installed.
how will this single jre run on all OS
It won't. You will bundle a separate JRE for each platform you want to distribute for. But JRE's for all platforms are still freely available, and the same Java code will still run on a JRE for any platform.
The module system doesn't influence the OS independency of java in general. Java applications that make use of the module system need to be run in a JRE. This can be either an OS specific pre-installed JRE as usual or a tailored runtime image (application embedded JRE) created with JLink.
The module systems main purpose is to provide you a managed way to split your application into different logical modules. E.g. into different .jar files that can be loaded at runtime - no matter on which operating system.
In summary, you have the following options:
Make sure that your client has the right JRE pre-installed. This could be dangerous, because (normally) you are not in control of his updating behavior.
Ship your application together with an official JRE.
Tailor your own, application and OS specific runtime image using JLink. Ship it bundled with your application.
But, suppose I do not know what OS my client would be running so how
the server will decide what image he should give to him. i.e., a Mac
Image, a Linux Image or a Windows exe.
You have to know the target OS and deliver the right runtime image.
While Java 9 makes it easier to ship a JRE which is more compact and specific to the needs of an individual application, you are not required to do so. If you were already planning to ship a JRE with your application it can be smaller with Java 9 than earlier versions.
It doesn't mean you have to ship a JRE, an application which wasn't shipped with a JRE is unlikely to start shipping with one now, and in fact Java 11 only ships as a JDK.
From this link on Java 9 features;
JLink allows you to create custom runtime images that only consist of your application modules and those JRE modules that your application requires. The result is likely a smaller runtime image, which uses fewer resources than a default JRE.
I have a confusion of JVM. does it comes with os or JDK ,because I have read in many books ,it comes with JDK but JVM is not plat form independent it means that it comes with the os .Can any one clear my doubt?
Think of the JVM / JRE / JDK as a car:
The JVM (Java Virtual Machine) is the engine of the car. It's the essential part that makes the car run. Likewise with Java, the JVM is the engine that can run Java bytecode. It's the interface between Java bytecode and the operating system and hardware of the computer.
The JRE (Java Runtime Environment) is the rest of the car. To be able to drive a car, you need more than just the engine (the JVM). You need a chassis, wheels, a steering wheel, pedals, etc. The JRE provides everything around the JVM, such as libraries and tools needed to run Java programs.
The JDK (Java Development Kit) provides extra tools on top of the JRE that you need when you want to write your own Java programs. It contains the Java compiler and other tools that you need to create Java class files from source code.
On Oracle's website, you can download either the JRE or the JDK.
If you are an end user and you just need to run Java programs that other people have created for you, then the JRE is all you need. It's a complete car - it includes the JVM (the engine of the car).
If you are a programmer and you want to write your own Java programs, then you need the JDK. Oracle's JDK package includes the JVM and JRE, as well as the Java compiler and other development tools.
The JVM is provided by Oracle (or by another party, if you are using a different Java implementation than Oracle's). It is not normally included with the operating system.
I know Java Virtual Machine is used to run a java program independent of the OS.But can anyone tell me if JVM is the part of OS or a package.
Depends on your definition of OS, your definition of package and your actual OS.
Some OS vendors ship a JRE or JDK as part of their OS install in eigter core or extended sets. They all ship it as packages. This includes AIX, Solaris and some of the commercial Linux distributions.
However, without knowning your background it is safe to say it would be a stretch to consider Java to be a part of the OS itself. The one exception would be Android (however it is debatable if the Android VMs can safely be called Java :)
(Not talking about embedded and card os here).
Java is platform independent, because in it code firstly compiled and then JVM convert into the code which is understand by OS. So I have doubt, do I need to install JVM on each OS or not?
The OS does not understand the code without any translation layer. The JVM understands the code. You need to install the java run time (JRE) which runs the JVM on any machine you wish to run java code. This is due to that java is not "native" code like C or C++ instead something is needed to convert the instructions to machine code which the JVM does.
When java compiles, it not compiled directly into machine code. It's compiled to bytecode - this bytecode cannot be read by a computer by itself, it needs some other program to come in and interpret the code for it. This is where the JVM comes in, it takes the bytecode and then turns it into machine code that the computer can execute.
So you need a JVM to run the bytecode produced by a java compiler, although you don't have to specifically have one installed. It is possible to bundle a JVM with your java program, so when a user downloads you program - they have java installed along with it.
This has the major benefit of having a java application able to run on any operating system that can run a JVM. This is why java is write once, run anywhere.
Yes you need to have a running jvm in order to execute .class files. Here's a relevant SO post that goes into more detail.