How to find native instructions generated from class file - java

I would like to learn what native instructions Java's JIT compiler generates when it loads a class file. Is there any way of knowing it?
I am working in Linux on a 586 processor. And I am using Sun's JDK 1.6 update 21. Is there any tool that I can use to find out what I am looking for?

You probably need -XX:+PrintOptoAssembly, but you'd need a debug build of the JVM. The links to the binary distributions seem not to be available any longer, so you might have to build it from source: http://download.java.net/jdk6/6u10/archive/
If you're planning to try this with OpenJDK 7 as well, this might be of interest:
http://wikis.sun.com/display/HotSpotInternals/PrintAssembly

Related

Is there a possibility to run a jar file within an android application with another jdk?

Im new to app development and was wondering if it would be possible to include another jdk. For example java 17 and run a jar that needs to be run in this exact version? I think termux has an api that you may use for these purposes but is there another possibility? These jars are only console based and without any gui.
I first tried to use the jar as a android library inside the app, which did not work since it was compiled in java version 17. I tried decompiling the jar and recompiling it with the version I was using. The problem is that all the libraries used are on other versions than I need them to be and so I could not recompile it. I also thought about writing the application in c# but I am not used to it at all, which would throw many new problems that I would be willing to take if it would be a possibility to work out my project.
Q: Is loading the code into your Android application an option?
A: No.
Android loads code from ".dex" files not ".class" files.
The ".class" files would need to be translated using dx.
The Android dx command doesn't understand Java 17 ".class" file format.
Also the code in the JAR is likely to depend on classes in the Java SE class library that the Android doesn't provide.
Q: What about running it in a separate Android VM?
A: No.
An Android VM requires ".dex" files; see above.
Also, the Java SE class library issue; see above.
Q: What about launching an OpenJDK or Oracle Java 17 JVM on the Android device to run the JAR?
A: In theory Yes, but in practice No. As far as I am aware, there is no port of OpenJDK Java SE to the Android OS platform.
Q: What about using Termux?
A: OK ... that might work. See Is it possible to install the JDK on an android device?.
I have no experience with this, and don't know what problems you may run into doing this. But I suspect that you wouldn't be able to distribute something that relies on Termux via the Google Playstore.
Another option is to download the source code1 for the application and try to build it in your Android dev environment
If the code uses Java classes / packages / libraries that are not available for Android, recode the relevant parts of the application to use Android equivalents instead.
Ditto if the code uses Java language features that are not yet supported in Android Java.
It probably won't be easy. It may turn out to be impractical.
1 - You said in a comment that the code your are trying to use is "open source". So the "download source and build it" option is available to you. I'm puzzled why you tried to decompile and recompile it instead ...

Run Java packages on unsupported Java Version

I have a PowerMac and it is giving me bad version number on some .jars. I would love to make it seem like I am running Java 6. How would I spoof the version? Let me also say I am running PowerPC and Leopard
The most likely problem is that you have Java 6 JAR files and you are trying to run them on an old Java installation.
How would I spoof the version?
The answer to your question is that you can't. The way to run Java 6 specific JAR files it to use a Java 6 (or later) JRE or JDK.
The problem is that the format of Java class files has changed, and your installation can't cope with the new format. And this is not a gratuitous change that you can pretend doesn't exist. Java 6 (actually Java 5) has support for generic types, enums, annotations and other things. Assuming that the JARs contain code that uses these new language features, an older JRE simply won't know what to do with them.
There are two solutions:
Upgrade your Java installations to the required level on all machines. This is the best solution ... if it is an option ... because it means your users will get the benefit of security and bug fixes and performance enhancements. (And progress of your project won't be held back by the constraint of supporting legacy platforms.)
Compile all of your code for compatibility with the oldest version of Java that you still have to use. Either compile on the corresponding old JDK, or on a more recent JDK using appropriate -source / -target / -Xbootclasspath options ... as described by the javac manual page.
The catch with the second solution is that if the source code for the JAR files in question uses recently added Java language features or APIs, then recompiling for the older platform will fail. To fix this you will need to rewrite your code to replace the nice modern stuff with archaic stuff. Not a good solution, IMO.
The other possibility is that you are seeing corrupted JAR files. This is unlikely, but it can happen if you are using applets or webstart, and the server is delivering error pages instead of JAR files.
The third possibility is that you simply haven't configured your Mac's Java installation's correctly. Making Java 7 the default should allow you to run everything without class version problems. (Thanks #paulsm4) Note that I can't help you with that ... 'cos I don't use Java on a Mac.

Can a Java program be analyzed to strip it down to essential packages as well as the JRE?

Is there some tool that can analyze a Java program and strip down the Java runtime and the program itself to the essentials only? Can a tool analyze the programs dependencies and create a custom JRE just with the required libraries?
You are looking for ProGuard. They claim it also works on rt.jar from the JRE.
You could try http://proguard.sourceforge.net/. It allows you removed unused classes and methods. This can backfire if you're using things like Class.forName("myclass"), so be sure to test a lot. You could try to run the shrinker against the JVM libraries, but that seems risky.

How to create native binaries for your Java app? [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 3 years ago.
I'm wondering how to package a Java application into a native binary for Windows, Linux and Mac OS X.
I know Minecraft does this, but I can't figure out how. This is what'd I'd like to do:
From NetBeans (preferably) or Eclipse, build the three binaries automatically.
Include native libraries for OpenGL et. all.
Obfuscate my code if possible.
If there's some way to mimic the Minecraft auto-updater feature, that'd be totally awesome.
So, are there any tools available to do this for you, or do I need to write a large bulk of XML to accomplish this?
To make a native binary for Windows, you would use a tool like Launch4J. On OSX you could use JarBundler. Minecraft simply distributes the jar file for Linux. I'm not aware of a native binary packager for Linux.
You could also compile your Java code via GCJ but that's probably not what you want, as there are limitations and compatibility concerns there. The native bundlers like Launch4j and JarBundler simply wrap your jar file and use a real JRE to execute it.
As for integrating with NetBeans or Eclipse, you'll probably have to write your own ant build file, especially since the solution varies from one platform to the next.
If you are using Java 9, you can also use Java 9 Modularization & jlink to ship a zero-dependency native app.
There is also maven-jlink-plugin that could help here.
Take a look at GCJBuilder plugin for eclipse. Not sure if it supports cross compilation as the command GCJ compiler does.
If the app. has a GUI and can be distributed from a web site, look into Java Web Start. JWS is supplied by Oracle, and provides auto-update amongst many other features.
Note that JWS uses Jar files, so no conversion is necessary.
I've used JSMooth for this in the past: http://jsmooth.sourceforge.net/
As mentioned before, this wrapper just looks for a real JRE to run it - it does not come with a bundled JRE.

Where to find Java 6 JSSE/JCE Source Code?

Where can I download the JSSE and JCE source code for the latest release of Java? The source build available at https://jdk6.dev.java.net/ does not include the javax.crypto (JCE) packages nor the com.sun.net.ssl.internal (JSSE) packages.
Not being able to debug these classes makes solving SSL issues incredibly difficult.
there: openjdk javax.net in the security group
src/share/classes/javax/net
src/share/classes/com/sun/net/ssl
src/share/classes/sun/security/ssl
src/share/classes/sun/net/www/protocol/https
also on this page:
src/share/classes/javax/crypto
src/share/classes/com/sun/crypto/provider
src/share/classes/sun/security/pkcs11
src/share/classes/sun/security/mscapi
These directories contain the core
cryptography framework and three
providers (SunJCE, SunPKCS11,
SunMSCAPI). SunJCE contains Java
implementations of many popular
algorithms, and the latter two
libraries allow calls made through the
standard Java cryptography APIs to be
routed into their respective native
libraries.
I downloaded the src jar from: http://download.java.net/jdk6/source/
NOTE:
This is a self extracting jar, so just linking to it won't work.
... and jar -xvf <filename> won't work either.
You need to: java -jar <filename>
cheers,
jer
if you just want read the source code:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/security/ssl/SSLSocketImpl.java
While this doesn't directly answer your question, using the javax.net.debug system property has helped me sort through SSL issues. -Djavax.net.debug=all pretty much gives you everything in gory detail. Documentation on this is at JSSE Debugging Utilities.
One note: I've seen that on Java 1.4 and maybe 1.5 levels, the output with option "all" is not as complete as it is using the same option on the Java 1.6 level. E.g., 1.6 shows the actual contents of network (socket) reads and writes. Maybe some levels of 1.4 and 1.5 do as well, but 1.6 was more consistent.
For some unknown reason Orcale doesn't released source.jar and javadocs jar for JSE.
I found only one place where you can find them http://jdk7src.sourceforge.net/ but it's outdated and unofficial.
The only one way is to clone OpenJDK repository
Put Jad on your system path. Install JadClipse plugin for Eclipse. Use the force, read the decompiled source. :-)

Categories

Resources