I am confused that for in the role of the JVM. Is my understanding correct that JVM only converts the Bytecode to Machine code? So essentially there is communcation between the OS, JVM and CPU. My assumption is that the bytecode gets converted to Machine code and then processed by CPU.
JVM is a virtual machine.
Its like when you install some OS(Linux, Solaris) on VMware or VirtualBox, those are virtual machines. They run on top of the host OS.
JVM is different in a way that it's machine code is bytecode. There are machines which have high level languages as their machine code. If JVM were to run on the hardware and not on the host OS then it would expect a hardware capable of using bytecode instruction set.
However since it's runs on top of host OS, any bytecode instruction is communicated to the host after being converted to machine code. In case of VMware/VirtualBox, these softwares take care of it. In case of JVM it's done by JRE.
JAVA is a compiled and interpreted language and not only just the interpreted language. Lets see step by step execution of its program.
Suppose we write a java program and save it as “hello.java” in a folder somewhere on a hard disk.
The name of the java compiler is “javac”. For its compilation we have to write “javac hello.java” on the command line.
At this stage the high level code gets converted to the machine level code. Java converts it into a .class file as “hello.class” . This code is also known as a byte code because every instruction in this file is of 1 Byte.
This byte code can also be taken away on any other platform like
Mac, Linux or Windows. It only takes a JVM of respective OS for its
execution. Hence, JAVA is known as a first platform independent and
architecturally neutral language.
Now, for its execution, interpreter is used. It is named as a “java” itself. For its interpretation, “java hello.java” should be used on the command line. It internally invokes class loader which is responsible for loading the ‘hello.class’ file from hard disk to JVM’s address space.
Here there comes a ‘Byte code verifier’ which verifies the code to avoid run time failures. After successful verification, JVM executes the byte by byte code with the help of OS.
Thus the whole java program gets executed by JVM and OS. While JVM acts as a mini OS for the java program and it concerns OS only when the instructions are not in its scope.
Related
a JVM implementation is an interpreter that converts a bytecode to machine code. But in the meantime a JVM implementation throws runtime error. Does it mean that an interpreter checks runtime error in Java? Or does it mean that Java's runtime occurs during the stage from bypecode to machine code?
It means that during run time of the application (when app was actually running) some exception was thrown eg attempt to divide by 0 which happen to be an user input.
After research, I've found that a classical compiler converts source code to machine code, ready for computer to execute it, while a classical interpreter directly execute source code.
In C, the compiler converts its source code to almost machine code (an object file). When we "run" a C program, it means the machine execute the object file. So the runtime error is thrown by the machine.
While in Java, the compiler converts its source code to bytecode, an intermediate code between source code and machine code. After compilation, an non-classical interpreter (an bytecode interpreter) directly executes this bytecode. So the runtime error is thrown by this kind of interpreter, which is called "virtual machine" in Java, as it acts like the machine in C which execute the program.
Return to the question I asked, the interpreter (JVM) in Java throws runtime error because it acts like machine itself rather than acting as an intermediate stage before the actual execution.
From Java In a Nutshell
jps provides a list of all active JVM processes on the local machine (or a remote machine, if a suitable instance of jstatd is running on the remote side).
What does "JVM processes" here mean,
processes of the underlying OS which run JVM exactly, or
processes that are supported by Java via Process, ProcessBuilder and Runtime.exec()...
Thanks.
A running Java Virtual Machine (JVM) instance is materialized by a native process launched by an OS while a Process class instance is a native process launched by a running JVM.
As each Java Virtual Machine is associated to a specific native process, instead of saying a process that runs a JVM, we could so shortcut it by a JVM process.
JVM stands for Java Virtual Machine.
In plain English, a virtual machine (VM) is any piece of software that simulates a real machine. There are two kinds of VMs:
system VM
process VM
A system virtual machine provides the functionality of a real computer. A process virtual machines allows to execute programs in one specific programming language. The advantage of a process VM (sometimes also called managed runtime environment) is that it provides the same environment across different platforms.
JVM is the process virtual machine for Java.
Since of the main design goals of Java is portability, the Java language code gets compiled to an intermediate representation called Java bytecode that can be executed on a JVM.
So, basically every time you run Java code you start an own JVM process.
Image source: Introduction to Computer Science using Java
There are exceptions to that, namely special-purpose processors that can interpret Java bytecode: with such processors Java is executed directly on the hardware without using a virtual machine. An example are ARM processors endowed with the Jazelle DBX direct bytecode execution.
I am a java beginner. i just read a line "
JVM (Java Virtual Machine) is an abstract machine." can anyone please help me to understand term abstract machine. what is abstract machine.
JVM = JAVA VIRTUAL MACHINE:- The word virtual itself indicates that it doesn't exists PHYSICALLY.
Elaborated here:
Abstract means HIDDEN.
When assembly programs runs on a computer, it is executed in the system runtime environment:
Properties
Platform dependent (if compiled for windows, a program will run only in windows not in Linux/UNIX etc.)
Not portable (same as above)
Systems runtime (in user's PC mainly under OS's control)
When Java Program runs into a computer, it is executed in another virtual machine (JVM) for which, the runtime environment is provided by JRE (JAVA Runtime Environment), it is installed automatically when you install JDK (Java Development Kit).
Without JRE, it is impossible to run Java Programs (Update: You can bundle your custom JRE with your code, in that case no need to install JDK or JRE separately but only in JDK9 & above)
This JVM itself runs in the system runtime (in user's PC/OS) but when Java program runs it is loaded into this running JVM.
For more practical AND visual experience:(for Windows only)
Open Task Manager
Go to Processes Tab
Find Java(TM) Platform SE Binary (This is JVM) - Java Instance
And now run a java program and write some code to delay the execution, like multi-threading with wait and notify (you can find such programs on google), due to this running program your Java Instance will show a little high memory and disk usage (not much high but slightly higher than that when no program was running in JRE/JVM). These processes you see in Task Manager, are running in System Runtime, and your Java program will not be listed there.
Instead it will be running inside this already running JRE.
This is the reason why JVM is ABSTRACT.
Now, do a little cross check and prove it..
Run 2-3 java programs, either keep them in longer waiting or just write Input Scanner and don't provide input, program will continue to run in blocking state so that we can see them later.
Once gain, confirm that only one instance of JVM/Java is running in task manager (Depends on how many JREs you have installed, sometimes IDEs can also create one instance, so better close it first for clear observation)
Now, where are those 2-3 Java Programs running?? Open VisualVM (it's under the same package/folder where your java executable resides)..
In this VisualVM, you can clearly see that, all your RUNNING Java Programs are listed.
Now, open side by side windows... Task Manager, VisualVM and one of your Running Code's Console.
Verify,
in Task manager - 1 instance of Java Binary.
in VisualVM - 3 different instances of your programs (if you run 3 java porgram)
Now, provide input in one of your code so that blocking state goes away and program terminates successfully.
Now verify,
in Task Manager - still 1 instance of Java Binary.
in VisualVM - 2 instances, because 1 code terminated/finished.
So, all your Java programs run under a Virtual Machine, a machine that is hidden, physically not available, abstract.
An abstract machine is a machine that does not have a physical existence.
Abstractness
A bicycle has a physical existence. I can touch it. It is not abstract.
A computer chip has a physical existence. I can touch it. It is not abstract.
A JVM is an executing program ... and has no physical existence. I cannot touch it. It is abstract.
Machine-ness:
A bicycle is a machine for transporting people.
A computer chip is a machine for executing programs that are coded in the instruction set of the chip; e.g. Intel x86 machine code.
A JVM is also a machine for executing programs that are coded in the instruction set of all JVMs; i.e. Java bytecodes.
Abstract machine means all of java application run in JVM and JVM runs in different platform as Window, Linux, Mac ...
JVM is interface to java application can run all platform
It is a bit philosophical question. When java application is compiled, the compiler produces its own "assembly" code that can not run directly on a hardware, it needs JVM. So, JVM creates an environment for java code similar to that of a machine. In other words from java perspective it is a machine, but in reality is a program that sits on top of a computer's operating system.
It means not real machine, however, it acts like a machine. Not any machine but as a machine called Central Processing Unit(CPU).
You could understand this like, whenever you write java program, you are writing instructions for a machine, but this machine is abstract, which is called Java Virtual Machine(JVM).
For each Operating system(OS) there is a Virtual Machine so that you do not have to write different versions of your java program to different OS.The JVM that you have in your JDK will handle the translation to each OS.
Your java program passes these steps to be understood by the underlying OS.
.java (Source file) gets compiled by compiler and becomes .class(class file) and this will be passed to JVM to be interpreted to machine language.
Class file is the one that has instructions for JVM
By reading this article, I know that each java application will run in a specific Java Virtual Machine Instance. So if I execute the following commands("Java -jar test1.jar","Java -jar test2.jar", I will get two processes in the system. And If each command used the default heap size, for example, 256M. The total memory cost is 512M, is that right?
Also I have other questions:
Is the Java virtual Machine a daemon process, start up with the system?
When I execute "java -jar test1.jar", it will create an instance of Java Virtual Machine, then execute the main function. Does it mean every running java application is a sub thread or process of Java Virtual Machine?
Is each running java application individual, other application can not get variable, method, constant, etc, from this running java application?
If one running java application is crashed, will it affect other running java application?
PS: I googled and got lots of different answers, I was totally confused. Anyone who can help me on this kind of questions or even more depth of Java virtual Machine. For example, How it works.
The JVM is a standard process, just like any other. As such there's no implicit communication or state sharing between the two. Each will have their own heap, threads etc. If you kill one it won't affect the other.
What will get shared are the code pages of the JVM itself. The kernel is intelligent enough to identify the same binary (any binary -not just the JVM) running twice and reuse the image. This only applies to the actual binary code - not its state. See here for more info re. Linux.
The JVM isn't a daemon process, but could be started upon system startup as a Windows service or Unix/Linux process (via /etc/init.d scripts). This is how you'd (say) run a web service written in Java when a machine is booted up.
1) No, but there a ways to launch java applications as services with wrappers (Google for "Java service").
2) Yes.
3) You can use communication between processes (v.g. HTTP). But there are no shortcuts due to all processes being run in JVM.
4) No
For the OS, JVM like an user application. Each JVM Instance is individual.
No. JVM is normal process as others.But you can run it as deamon process.
Yes. Java application run on JVM just like you application on OS.
Yes. Each JVM thread is individual, but they can communication whith other JVMs through network,RMI...
It depends. Normally they are individual, but if a JVM crash cause the OS crash, other JVMs will be effected.
I installed scala-2.8.0.RC3 by extracting the tgz file into my cygwin (vista) home directory. I made sure to set $PATH to scala-2.8.0.RC3/bin.
I start the REPL by typing:
$ scala
Welcome to Scala version 2.8.0.RC3 (Java HotSpot(TM) Client VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
Now when I tried to enter an expression
scala> 1 + 'a'
the cursor hangs there without any response. Granted that I have chrome open with a million tabs and VLC playing in the background, but CPU utilization was 12% and virtual memory was about 75% utilized. What's going on ? Do I have to set the CLASSPATH or perform other steps.
There is an enormous start-up cost for the REPL (which includes the compiler, of course), but it does not use fsc, it is self-contained within a single JVM. Using it gradually causes JVM bytecode to be converted to native code, after which it's very fast.
Have you tried sending Ctrl+Break to your Scala process ?
For a Java process this will force a dump of all the thread states and you'll be able to see what each thread is doing, what it's waiting on, if it's deadlocked etc.