83

Is JVM open source code? If not, how can I get the code of JVM?

Smi
  • 13,850
  • 9
  • 56
  • 64
giri
  • 26,773
  • 63
  • 143
  • 176

4 Answers4

68

It depends entirely on which JVM you use.

If you use the OpenJDK JVM, then you can get the source code from here (or here from a list of OpenJDK projects, or here from a list of GitHub mirrors).
If you use the Kaffe JVM, you can get the source from here.
If you use the Sun JVM version 6 or later, then you can get the source from here.

If you use a Sun JVM earlier than 6, then you can often get the source under an academic license. If you use an IBM, Oracle, HP, or other JVM, then the source is not open.

Update May 2013

The Version 6 source can still be accessed by the above link, or it can be accessed via this link. This latter link also includes a handy genealogy table that shows how the Oracle JDK and OpenJDK versions match with each other.

Additionally, a more up to date version of the Java 7 source can be found here. This also includes the fixes for the releases of Java 7 since GA.

And, of no surprise to anyone, the Java 8 sources can be found here.

Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
40

Have a look at hotspot JVM here: http://openjdk.java.net/groups/hotspot/

peterh
  • 11,875
  • 18
  • 85
  • 108
D.C.
  • 15,340
  • 19
  • 71
  • 102
  • 4
    However, the best place to start leading how Java run is the src.zip and only after you are familiar with most of the key classes there would you move on to the hotspot module. – Peter Lawrey Jan 25 '15 at 08:59
  • 1
    @PeterLawrey May I ask you some? After looking at the `src\share\native\java\lang\Thread.c` I figured out that the implementation of the native method `interrupt0` is dug in `JVM_interrupt`. Where can I find the source for it? Is that from the hotspot? – St.Antario Sep 30 '15 at 07:46
  • 3
    seems the repository browsing link there doesn't work. This does: https://github.com/openjdk-mirror/jdk7u-hotspot – eis Oct 13 '17 at 19:52
  • 1
    Answers should not consist out of a mere link – phil294 Aug 16 '18 at 06:04
1

The core part of the JVM is in the hotspot module of the OpenJDK. However what you need is more likely to be in src.zip.

The hotspot module apart of those classes is

  • mostly in C++
  • not always easy to understand. This has improved over the years and new code tends to be better as they are more aware that the code will have broader consumption.
  • often not what you are looking for.

For this reason if you want to know how the JVM runs it is best to look at the commonly used classes. For example, even something as low level as how lambdas really work at runtime is mostly in the src.zip not much is in the JVM.

Most of the source for the libraries come with the JDK in the src.zip file. Your IDE will use that automatically. You are much better off being familiar with the classes in these libraries than playing with the JDK itself.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 5
    giri was asking about JVM, not JDK. – Michael Graczyk Jun 14 '12 at 21:14
  • 2
    Yes he wanted the JVM and not of the JDK source, so you get now a down. – peterh Jan 23 '15 at 20:39
  • 1
    @PeterHorvath 90% of the JVM is in the sources, and 95% of the useful code is in the src.zip. If you don't know about the OpenJDK, you should be starting with the sources which are intended for general consumption. – Peter Lawrey Jan 24 '15 at 21:34
  • 2
    I double checked again, only for you. No, the sources of the _JVM_ (the so-named hotspot JVM, the java bytecode interpreter) isn't in src.zip . In src.zip, is there only the source code of the _JDK_, which is the standard system library collection. You really don't understand the difference? – peterh Jan 24 '15 at 21:49
  • 1
    @PeterHorvath Ok, try running the JVM without using any of the classes mentioned in the src.zip. Let me know how useful your definition of JVM is. – Peter Lawrey Jan 24 '15 at 22:03
  • 2
    The OP asked for the source code of the JVM. It is not there, it is in a mercurial repository by the oracle's public web. Have you ever seen this src.zip? In your life? Ever? – peterh Jan 24 '15 at 22:06
  • @PeterHorvath Not sure how to give a sane response to your comment. I think you know the answer to those questions and have given up trying to justify what you are saying. – Peter Lawrey Jan 24 '15 at 22:11
  • 2
    I think there is a very simple sane answer: "I am sorry, maybe I really misunderstood this question before 4 years", and then I answered, "no problem, bro". I was only upset after finding this question by google, and finding 4 BAD answers in a row, all of them from old, 20k+ SE users. – peterh Jan 24 '15 at 22:12
  • 1
    @PeterHorvath In the last 4 years I haven't changed opinion that the first place to look at the code run by the JVM, code it cannot run without is src.zip. Most likely what you need to know is there esp if you are a user who doesn't even know OpenJDK source exists. You could say the hotspot module is the true essential JVM but chances are whatever they are interested in is either a) not there, or b) in C++, c) too difficult for even an advanced Java user to to understand. You have to consider the context/level of the OP when picking the most appropriate answer IMHO. – Peter Lawrey Jan 25 '15 at 08:35
  • @PeterHorvath I do appreciate the need to clean up old bad answers, I do this myself from time to time. I have probably edited a several hundred questions and answers and I have more than a few people fix my English ;) – Peter Lawrey Jan 25 '15 at 08:38
  • @PeterHorvath You have convinced me that I needed to clarify why I think srcc.zip is the place to start. – Peter Lawrey Jan 25 '15 at 08:58
  • @PeterLawrey I agree that understanding most used Java classes will itself take years and digging into JVM may carry me away from prime objective of development with Java language.But understanding of how the JVM works and how the JVM is built and came into being will give me a broader perspective and will allow me to appreciate the complexity of what is really underneath and enlighten me with various aspects.I will be comfortable if many things are not black box. In the process of understanding JVM, knowledge about OS and CPU arch is also preferred.It is just for intellectual stimulation. – user104309 Nov 17 '17 at 22:00
  • @PeterLawrey Having explained my curiosity for JVM internals and necessity for mind satisfaction that everyday I know a little bit more of what lies beneath, I request you to provide with some materials where there is pointing learning of this JVM with source code with correct sequence of topics instead of trying to learn by searching for topics in wrong order which will not make sense for beginners and finally give up. As you said, it is going to be too difficult for me as I lost touch with C/C++ and still there is an ocean of Java itself left unexplored. – user104309 Nov 17 '17 at 22:13
  • @PeterLawrey I have a few articles of Ben Evans of JCP about Hotspot JVM architecture. But it is overwhelming or impossible for me to connect the dots to make a whole picture. I am not able to figure out head/start and tail/end of JVM when reading an aspect of JVM as in at what part/time of the execution, this particular concept of JVM is being used. It will be interesting and satisfying to learn, which portion of source code of JVM understands bytecode, on call of `main()` what all/first thing it does, object map, what it does when it encounters `native` keyword like in `Thread` class. – user104309 Nov 17 '17 at 23:16
  • @user104309 to see what native methods do you need to find the method in the C code which implement the matching JNI signatures e.g. there will be a java_lang_Thread_yield method or something similar. Note: methods like this call the OS to do the real work so there isn't much to see in the JVM itself. – Peter Lawrey Nov 19 '17 at 22:39
-3

There is no open source jvm even if there were you can't bypass Oracle's stupid classpath exception. In short openjdk is still tied to $$ driven scheme that forces you to contend with a comercial vm. No different from Microsoft really, you can work with C# under what ever os they even provide .net libraries free but maintain control over Visual Studio which practically forces the end user to make use of Windows as the chosen environment.