I want to learn the internals of JVM. For this purpose I chose Jikes RVM to work with, but the problem is that I am not able to debug the source code as it doesn't support it.
My question is that is there some open source JVM which can be debugged to see how it works with class files. I am in real need of some good information about it.
Thanks
Well, since the Oracle JDK is open source, this might be a good place to start: http://openjdk.java.net/
The internals of the JVM is different between JVM's and knowledge gained from one may not be usable for other JVM's.
If you want to see how the runtime library (all the java.* classes) is implemented, there is a src.zip in most JDK distributions (not the JRE ones). If you use Eclipse, set it up to use that JDK as the JRE and you can navigate directly around in the various classes starting from your own program.
Related
I've been looking around for an answer to this but haven't had much luck.
I've been working on a game in java that uses the javax.util.JavaCompiler class. I've discovered that this does not work with the JRE as I'll get a Null Pointer exception when I call the compiler run method (I'm assuming they simply don't ship a compiler with the JRE which is totally understandable).
My problem now is that my game inherently is dependent on the JDK. Is it possible to force my game to somehow search for the system environment variables and rather execute itself using the jdk?
I've tried using a separate launcher that then searches for the jdk and executes the game through that but that feels messy and I'm concerned there could be factors I'm missing.
It is technically possible, but it would make your application slow to start up to go an exhaustive search. AFAIK, there is no supported way to do this.
It is up to the user to choose how he / she runs a Java application. Doing clever things like searching for a JDK installation is potentially dangerous. (You could end up choosing an old insecure JDK that the user doesn't know about, or that they forgot to uninstall. That could open up the user to security breaches, etcetera.)
I would advise limiting yourself to a friendly message to the user explaining that they need to install and/or use a JDK.
You have the following options:
Check for the existence of that class during startup of your application; and if not found give your user a clear error message that he needs to run with a JDK (you would also specify that somehow as "prerequesites" on the site where people download your application).
Step back and carefully analyse which classes exactly your solution depends upon. And then you either repackage exactly those classes; or maybe you simply ship the corresponding JAR file with your application.
You see, asking your users to install a JDK is a big burden for them. Even when you are talking about some "inter-company" product where people have to use it ... few people are willing to install a JDK. And you absolutely should not expect that many users have a JDK installed.
On the other hand, when you consider re-packaging that stuff; you also have to look into the legal aspects; you don't want to violate any licence rules by simply handing out JDK related JARs to your customers.
You can simply put a try/catch block for the NPE around your use of the compiler API, and, in the event of the error, inform the user in no uncertain terms of the requirement.
Or, you can use asm or some other code generator package to avoid the need for the compiler.
Well I found the solution to the problem. Thank you to everyone who said I should package my game with a compiler. I found this post where the author describes rather using the eclipse compiler. Apparently javax.tools.JavaCompiler is just a common interface provided, I really should learnt the API in more detail.
You don't need to run your app with JDK. You can still run it with JRE. All you need to do is to make sure that a jar (from JDK or from anywhere else) that contains needed classes for your app is in your classpath. You can achieve it by including needed JAR(s) into your installation or by including the path to needed jars in your classpath parameter (-cp) when you run your app. If you want your app to run on computer where only JRE is installed you will have to include needed JAR(s) into your installation. think of it as any other third party library that you use and thus you have to package those libraries JARs into your application JAR/WAR
I have a question which I'm pretty confused from.
I am aware of the differences between Java Runtime Enviroment and Java Developement Kit.
I'm writing a program that uses the ToolProvider.getSystemJavaCompiler() method to compile java code from within the code.
Now, I've been answered that I can't compile code from client side if my client doesn't have JDK installed. My main question is, how can I do that? I don't want my clients having to install JDK on their computer just to run my program.
Thanks in advance!
You need to compile it on your system, and distribute the class file of corresponding java source file to anyone.
That class file doesn't require JDK but JRE must be installed on that system to run the class file.
If you want to compile code, you need a compiler, so if the user can't be expected to have the compiler you need, you'll simply have to bundle it.
I really can't say I know how to bundle the standard javac compiler, though it's probably possible, strictly speaking, to find the Jar file that contains it and bundle that along with your code. No idea how robust such a solution would be, though.
But depending on your needs, you may not need the standard javac. There are tons of byte-code generation libraries out there, with more or less high-level functionality. I wouldn't really want to recommend anything that I have no personal experience with, but examples include Byte Buddy or ASM. You could probably use ABCL too.
Eclipse's compiler is worth a look as well.
There is also an so question here.
So there really is no way to do what it is you are wanting to do unless you bundle the compiler itself with you application, or unless you find a library that has all of the Java compiler code in it already so it doesn't have to use the JDK compiler, you will not get what you want, and what you want is the ability to turn a String containing source code into a Java class.
I do not understand what you wish to accomplish, but the BEST option I can give you is asm. If you are up for the task, you can manually write new classes at runtime without the presence of the JDK compiler. HOWEVER, this does not involve you using a String full of source code and turning it into a Class object. This is you working at the low level with the Java bytecode for the most part.
This tutorial can get you started:
https://www.javaworld.com/article/2071777/design-patterns/add-dynamic-java-code-to-your-application.html
And here is the Java documentation for class files. You can use this to expand on what you learned from the first link:
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html
That is the only instance creating classes on the fly that I can give you. That being said, you could try writing your own Java compiler that can turn source code into classes without ever getting the Java compiler, but at that point you are literally recreating the Java compiler yourself, and I assure you that is no easy feat for one person.
How does one start a Java VM from C? Writing the C code seems to be straigtforward -- I've been following the code that appears on p. 84 of Liang's "The Java Native Interface". It's the linking process that has me stymied. Liang's book is 10+ years out of date in that regard and I can't find anything on the net which addresses this goal (and which works).
To be clear, what I want to do is launch a standard windows program (written in C), which then launches the JVM and calls a main() in a Java class (which I have written). This program should not rely the presence of jvm.dll or jvm.lib -- the user shouldn't have to install Java to run the program. Maybe this isn't possible without an unreasonable amount of effort.
The development environment is MinGW under windows. I'm able to link in such a way that the program works when the .dll is in a separate file, but not in a way so that there's only a single executable without any .dlls or .libs.
In hindsight, I can see now that this was a dumb question, or at least one that hadn't been thought through. The moral of the story is that the "JVM" is not a single executable, or even an executable plus some JAR files; the JVM relies on a slew of independently stored files with various mutual dependencies. Unraveling all of these relationships so that they could be brought into a single file (or even two files) would be a massive undertaking. Thanks for the knock in the head.
So, to be clear - you want to launch a JVM without the requirement of a JVM being present? How do you propose to accomplish that? Unless you're contemplating writing your own JVM implementation (which I'd say falls under the category "unreasonable amount of effort"), having a JVM installed is a reasonable requirement. Assuming that, you can just spawn a java process and include the appropriate command-line parameters (classpath, class to run etc).
Disclaimer: I don't think that having a Java runtime installed is unreasonable for users. That said, I do understand your motivations for a low-friction install for users.
Using the Sun JRE is probably not going to be fruitful here. In theory, you could grab the Sun JRE, modify it to build as a static library instead of a DLL and figure out a way to cram all the resources that get bundled with it (fonts, images, cursors, SSL certificates, localized message files, etc.) into a single resource and then modify the runtime to load from there. But this is almost certainly an "unreasonable amount of effort."
You might want to look at GCJ instead: its architecture is different than the Sun JRE which lends itself more to being embedded in another application, or it can compile your Java to native machine code.
(Also, do check the licensing to ensure that you can properly redistribute this no matter which route you take.)
when you write an app in java, people need to have this java installed to use it right?
Sorry if this is a dumb question.
Traroth is correct; your audience needs the Java Runtime in order to use your java.
However, they don't necessarily need that Java, or in other words, they don't need to be using Sun (now Oracle)'s JRE. They could also be using the OpenJDK, or any of the other free java implementations.
Not a silly question at all.
Side Note: As Glenn Nelson stated, there are Ahead-Of-Time compilers, and if you want to go down that route, go ahead. But I'd strongly warn against jumping into that boat, especially if you are just starting off in Java. AOT Java compilers come with their own set of issues.
Yes, you need the Java Runtime to run a java program:
http://java.com/en/download/
There are also executable installers which may bundle Java or download it during the installation. You may also use "Java Web Start".
Want to create animation dll for Window XP
Is it ok to create Java2d animation and export as dll??
Yes. You need to write code in C++ to start the JVM with the invocation interface to JNI, and call into it. However, you may find it difficult to create windows in this way that integrate seamlessly with your Windows environment application to display your animation. This is a rather advanced JNI usage, and I'd recommend reading the JNI book before even trying a little bit of it.
I am pretty sure you can only create .Jar files from java not dlls
I doubt so, unless there's some 3rd party tools out there. For your case where graphics is involved, chances are even lower.
Actually, what Quentin said should work. When you compile java to native with GCJ you first compile the .java files into platform specific .o (object) files. Presumably you would compile the .o files into a dll rather than an exe. GCJ also includes components like the garbage collector, and base java libraries. None of which require a JVM to run. The downer is that the dll would be huge. A simple "Hello World" app when compiled with GCJ is ~35MB, thanks to all the default libs and the garbage collector. Likewise your dll would be huge.
There are "bridges" that allow Java and non-Java code to call into one another. Depending on what you are trying to accomplish, these might be useful as you could write your Java code and then call into it from a C++ or C# DLL, depending on which language you are creating your DLL with, which will also determine what kind of bridge you need. I have never seen a freely provided bridge though. All the ones I've found when looking had to be purchased.
No, IIRC you can't. DLLs are linked directly when loaded. Java code needs a jvm, so you can only provide a dll that starts a jvm and starts code there, but not all necessarily stuff fits in the dll.
You should not do this. It looks like you're trying to use the wrong approach for your problem.
Well…
GCJ is available for Windows.
GCJ is part of GCC.
GCC can create dlls.
It might be possible to put that together to build DLLs using GCJ.
Yes, it is possible to generate DLLs from Java source code.
2 Methods I have used:
IKVM
Graal
IKVM is mature, but rather slow in runtime execution of the generated DLL.
Graal is fast, but early days and immature in the Windows environment.
See https://openjdk.java.net/jeps/295 for further info.
There are other commercial options available as well.
I agree with bmargulies. It's probably feasible for an expert, but it would be a large DLL and you'd be mixing technologies that were never made to work together. It doesn't make sense to try this, in my opinion.