Java bytecode specification [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Is there a nice place for learning the JVM bytecode instruction set. The specification perhaps and maybe some tutorials?
I ask because I would like to design a toy language and a compiler for it that generates JVM bytecode.
Thanks for your knowledge and perhaps googling.

A good reference for Java bytecode specification is the The Java Virtual Machine Specification.
See Chapter 4. The class File Format and Chapter 6. The Java Virtual Machine Instruction Set.

A little more "graphic" explanation, IBM developer works: Understanding bytecode makes you a better programmer.

The book Programming for the Java Virtual Machine explains the JVM instruction set and how to write code for it. It also introduces a bytecode assembler called Oolong, which I have not been able to download. You can, however, use Jasmin, the predecessor of Oolong. Essentially, you write a text file with instructions and Jasmin will spit out a .class file. The book was published in 1999, but it is still a good and gentle introduction to the VM.

Also useful are the javap disassembler and bytecode manipulation frameworks like ASM and BCEL, even if all you want to do is verify your classes.

The Java Virtual Machine Specification is a good place to start.
See Chapter 4 The class File Format. The updates cover new attributes added since the 2nd edition was made.

To start with, I suggest generating Java code from your language.
This will make reading and debugging much simpler.

A new version of the specification has been released:
The Java Virtual Machine Specification, Java SE 7 Edition
This new addition does exclude the chapter on Java Programming Language Concepts which folks may or may not find useful. See here for that chapter in the Second Edition of the spec.

Perhaps check out Preon's example on how to parse a Java class file. It has a fairly complete representation of the bytecode in a Java object model.

This is a bit more specialized, but here is an on-line presentation on how to optimize generated bytecode for running on the JVM. It was presented at the recent JVM Languages Summit conferences. InfoQ has a collection of presentations from that conference which might be of help to someone wanting to bring up a language on the JVM (or to see what's already been done).

Related

why we are calling java is a programming language and also platform [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've just confused and not having a clear idea to tell, Why java is called a programming language and also a platform. I've referred in oracle's corporate site:
Java technology is both a programming language and a platform
but there is no answer for my question.
Java is a programming language, just read the first line here
http://docs.oracle.com/javase/tutorial/
Java runs on a platform called Java Virtual Machine, which makes it platform independent.
Java is not quite called a platform itself.
Java is the programming language.
Java runs on the platform called the Java Virtual Machine (JVM).
Edit
To clarify and improve this answer following Roman Konoval's comment, the link provided by the OP (http://docs.oracle.com/javase/tutorial/getStarted/intro/definition.html) does indeed state
Java technology is both a programming language and a platform.
If I am understanding the question correctly, the OP is not distinguishing a difference between the phrases "Java" and "Java technology" in the question.
Based on that interpretation (feel free to correct me) and to answer his question using his own words, "Java" (AKA, in this case, "the Java technology") encompasses everything pertaining to Java - the programming language itself, the JVM (the platform), the JDK, etc. That is why "Java," as the OP phrases it, is, in fact, both a programming language and a platform.
java is a programing language not a platform. i think you are confused because of the way it is explained in the Oracle link you have mentioned.
java Programing language and the java platform(the platform on which java works) together called as JAVA TECHNOLOGY.
Java Technology = Java Programing Language + Platform it works on.
its not like java is platform. In general parlance, Java means the Java programing language.
Q: How java is Secure?
A: when we compile the java code JVM converts the java code into Byte Code, that Byte code is secure. If any changes made in that byte code it won't execute.
Java is a programming language and it is a platform independent, because every java program runs under the java virtual machine(jvm) platform. The jvm is a native to the java technology. So java is both a language and also a platform.
Note:
JVM is a Platform independent, for every platform we are having different jvm's. The compiled java code is convert to the machine code for the os running which the program.

Automatically convert Scala code to Java code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have an app written in Scala and some of my team members want a Java version of it. It is a demo app to use another API written in Scala, and they want a Java version of the app to be able to use the API from Java. However, the app is somewhat large and I don't want to manually rewerite in Java (and they don't want to learn Scala). Is there any tool that will automatically generate (readable) Java code from the Scala code?
they want a Java version of the app to be able to use the API from Java
Scala classes are usable from Java (since it's all JVM bytecode in the end). Can you just package a jar with your classes for them to use?
I don't think it's possible to convert from scala back to standard java since Scala does some pretty low-level byte-code manipulation. I'm 90% sure they do some things that can't exactly be translated back into normal Java code.
No, there is no such tool.
Scala in some sense is a coffescript of Java.
Look how all that funky classes in coffescript are translated to javascript. The same* would be with scala.
I don't think that there is a lot features that can't be translated to Java in any way, but most of the features will be translated to extremely cluttered code, even if the human will do that work.
But there is java to scala translators.
* not saying literally about classes
You have to decompile the .class files to java source files, beware that scala produces many more .class files
You can use javap from oracle included in the sdk or jad decompiler
for detailed explanation read the following article:
Link scala class to java source
Not sure if this would work, but you could run the class files through a java decompiler.
If it works at all the result is bound to be ugly as hell.

LLVM CIL and Java Bytecode backend [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I saw the http://vmkit.llvm.org/ project but it's not quite what I'm looking for. Don't want my code to run on yet another VM but on .NET's and Java's VM.
Are there any compiler backends for LLVM that generate .NET CIL and/or Java Bytecode?
LLVM is on a much lower level than CIL and Java byte code. This means that it is difficult to map the LLVM instructions on to CIL and Java instructions, and nobody has really needed it yet so the work has not been done completely.
Some of the work has been done though. See http://llvm.1065342.n5.nabble.com/JVM-Backend-tp41356.html to see if it is useful to you.
EDIT 2020-09-03: Since this answer was written, WebAssembly has been defined which is essentially a cross platform machine language definition which runs in all major browsers and hopefully soon also natively on Linux and others. I would think that the future would be that everything distributes in this format and then is run on the appropriate virtual machine. This will most likely end the CPU wars.
C++ can be compiled to CIL (with visual C++/CLI compiler), so why not a CIL backend for LLVM?
I don't think it would be so complicated as far as the not .NET specific CIL instruction set
is quite small/simple. Compiling C++ to Java is much more complicated because there are no pointer instructions in Java's bytecode, so a Java's bytecode LLVM backend would be much more complicated.
I guess guys from MONO LLVM backend have already worked on something similar, but it seems they exploited LLVM in a different way because MONO is a C# compiler not a C++ compiler.

Is there a way to transfer/translate the code written in Java to other languages? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Theoretically this seems possible to me. So can Any one confirm this to me, if it's possible? and if there is such a software that does this?(like Java to C++ or C#)
And in general would it be possible to transfer languages like Java to server-side programing language like PHP?
Translating the syntactical elements of one language and producing another is not trivial but it's not impossible. A good parser can build syntax trees in one language and then emit another. The difficulty of porting code outside the context of simple "Hello World" type applications is twofold:
The libraries of one language will probably differ (e.g. WinForms vs Swing)
Some language features will have to be catered for: (lambda expressions, anonymous methods, different inheritance implementations etc).
It is possible, but the major problem is that Java has a very large runtime library which needs to be made available in the target language in order to be able to do a fully automatic conversion.
For the special case of Java -> .NET, you can use J# from Microsoft to compile it into a .NET assembly which can then be used. Also ikvm.net allows for running a JVM inside .NET.
For PHP I do not believe such a solution exist. You MAY be able to use gcj to create a native library which can be linked in, but I do not believe it is a feasible soultion.
What functionality do you need in PHP?
Visual Studio ships with a Java to C# translator, and even tough it does a pretty decent job, there's still a lot to clean up afterwards.
In my experience you really have to ask yourself if it makes sense to translate code from one language to another. What is the gain? Will the translated code be maintainable? If the answers to these questions point in the wrong direction, translating is probably not the right approach.
Google Web Toolkit does conversion from Java to JavaScript:
http://code.google.com/webtoolkit/overview.html
to answer your question, yup, theoretically this is indeed possible and practically such technology is used every day :)
The interesting thing, in my opinion, is that the Java converters typically convert by taking the bytecode, not the source code. Then it's, say, bytecode-to-ObjectiveC source code. For some converters (at least one opensource one) it's bytecode-to-XML then XML-to-target-language.
For example, the Uniwar application for the iPhone, which has been acclaimed by all and made its way to the appStore's top ten, as been written in Java (JME) and automatically converted from the Java bytecode. Reaching the top ten, even for a few days, means that this is deployed on a lot of machines ;)
In the Real-World [TM], Cobol-Java and, weirdly, Java-Cobol are not unheard of.
For all this to work that said you need a really good converter :)
Theoretically it is possible. But as others pointed out the main problem is to translate libraries.
Some time ago I made Java to Tcl(XOTcl) and Java to Python translators to evaluate the translation posibility. Search by java2tcl and yava2python.
They convert syntax but do not make relevant constructions translations (e.g. Java file operations to Python ones). That would require more development time.
In general my opinion is what such a translation may be possible. But only if your translator covers classes/libraries of the converted project.

What are the best practices for the Middleware API? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
We are developing a middleware SDK, both in C++ and Java to be used as a library/DLL by, for example, game developers, animation software developers, Avatar developers to enhance their products.
What I would like to know is this: Are there standard "Best Practices" for the development of these types of API?
I am thinking in terms of usability, readability, efficiency etc.
My two favourite resources on the subject: http://mollyrocket.com/873 and http://video.google.com/videoplay?docid=-3733345136856180693
From using third party libraries on Windows I've learned the following two things:
Try to distribute your library as a DLL rather than a static library. This gives way better compatibility between different c compilers and linkers. Another problem with static libraries in visual c++ is that the choice of runtime library can make libraries incompatible with code using a different runtime library and you may end up needing to distribute one version of the library for each runtime library.
Avoid c++ if possible. The c++ name mangling differs alot between different compilers and it's unlikely that a library built for visual c++ will be possible to link from another build environment in windows. When it comes to C, things are much better, in particular if you use dll's.
If you really want to get the good parts of c++ (such as resource management through constructors and destructors), build a convenience layer in c++ that you distribute as source code that hides away your c functions. Since the user has the source and compiles it locally, it won't have any name mangiling or abi issues with the local environment.
Without knowing too much about calling c/c++ code from Java, I expect it to be way easier to work with c code than c++ code because of the name mangling issues.
The book "Imperfect C++" has some discussion on library compatibility that I found very helpful.
The video from Josh Bloch mentioned by yrp is a classic - I second that recommendation.
Some general guidelines:
DO define your API primarily in terms of interfaces, factories, and builders.
DO clearly specify exactly which packages and classes are part of the API.
DO provide a jar specifically used for compiling against the API.
DO NOT rely heavily on inheritance or the template method pattern - over time this becomes fragile and broken.
DO NOT use the singleton pattern or at least use it with extreme caution.
DO create package and class level javadoc explaining usage and concepts.
Take a look at Framework Design Guidelines. I know it is .NET specific, but you can probably learn a lot of general information from it too.
There are lots of ways to design apis, depending on what you are solving. I think a full answer to this question would be worthy off a whole book, such as the gang of four patterns book. For Java specifically, and also just OO programming in general, I would recommend Effective Java 2nd Edition. The first is general and a lot of popular programming patterns, when they apply and their benefits. Effective Java is Java centered, but parts of it is general enough to apply to any programming language.

Categories

Resources