Is it possible to view a Java class files bytecode [duplicate] - java

This question already has answers here:
Is it possible to view bytecode of Class file? [duplicate]
(5 answers)
Closed 7 years ago.
I am working on a bytecode manipulation/generation in Java and I was just wondering if there is an easy way I could check the bytecode. I do not want to decompile the file, I would like to actually look at the compiled bytecode. I do not need to edit it. Any links or programs for doing this would be acceptable answers.

Since you wanted to be guided to some program that can easily show you the byte code then my suggestion is to use IntelliJ IDEA since it has built-in support for viewing byte code.
Here's an example how to do it (it can also be mapped to some keys of your choice):
It is very easy, and it can surely be done in eclipse or NetBeans as well.

Not sure what exactly you need, but apart from javap, which is a command-line tool for displaying the bytecode, you can take a look at javassist, asm and cglib - they allow you to parse the bytecode with java code.

I've been working on a decompiler that has a color-coded bytecode output mode (which I find far more readable than javap). It can also output Java code or an intermediate 'bytecode AST'.

The javap command-line tool, which is bundled with Oracle's JDK, gives a detailed textual dump of .class files along with the constant pool and all functions' bytecode content. Just run it with -v to get a full dump.

Related

Convert String to callable methods in JAVA [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert String to code in Java
Dynamic code execution on Java
I have a String containing : "for(int i=0 ; i<5 ; i++){System.out.println(\"*\");}"
Can I execute the code in this String in Java?
Since Java 6, you can compile and run a Java compilation unit defined as a String or a File using standard APIs in the SDK (a compilation unit is basically everything that goes inside a .java file - package, imports, classes/interfaces/enumerations), take a look at this example. You can't run an arbitrary Java snippet like the one in your question, though.
If at all possible, it'd be a better idea to embed a different scripting language that allows you to run snippets of code from a Java program - for example, JavaScript, Groovy, MVEL, BeanShell, etc.
If you turn it into a full-blown source file, you can feed it to the java compiler programmatically, but last time I checked that was only available if you had the java SDK installed on your machine; it was not available on machines with the client distribution of Java. Of course, this may have changed since then. Look at package com.sun.tools.javac and you will find the java compiler API there.
Maybe you can run this as Groovy:
http://groovy.codehaus.org/Embedding+Groovy
There isn't a Java Core API function for doing this, but you can call javac either by using Runtime.exec or using some "unsafe" classes from com.sun.tools.javac Here's an example:
http://juixe.com/techknow/index.php/2006/12/12/invoke-javac-at-runtime/
I don't think you can execute a String containing a java code.
But it is worth a try if you can save that as a java source file and try to use ProcessBuilder class to execute.
Never tried it and not sure if it is best way to do it. So use it with caution :)
Good Luck!
Also found a similar post: Runtime class in java
No, you can not execute this code in your program.

java - Compile code on client side without JDK

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 can I achieve java byte code encryption (against reverse engineering) [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How to lock compiled Java Classes to prevent decompilation
...ideally combined with licensing?
Our product is ported from PHP to Java. With PHP, there was a great code encryption / license tool named IONCube Encoder. It encrypts the PHP source code an allows the execution only if a appropriate license is present.
Is there any equivalent for a Java application (that does not rely on a hardware dongle)?
Take a look at JET Excelsior. That's what we've been using and it's worked pretty well for us.
IONCube Encoder works by obfuscating your code, not encrypting it. If you want to get the same effect, there are a number of obfuscation tools for Java, such as
KlassMaster
DashO
You might also want to have a look at the proguard obfuscator

Understanding Java Byte Code

Often I am stuck with a java class file with no source and I am trying to understand the problem I have at hand.
Note a decompiler is useful but not sufficient in all situation...
I have two question
What tools are available to view java byte code (preferably available from the linux command line )
What are good references to get familiar with java byte code syntax
Rather than looking directly at the Java bytecode, which will require familiarity with the Java virtual machine and its operations, one could try to use a Java decompiling utility. A decompiler will attempt to create a java source file from the specified class file.
The How do I “decompile” Java class files? is a related question which would be informative for finding out how to decompile Java class files.
That said, one could use the javap command which is part of the JDK in order to disassemble Java class files. The output of javap will be the Java bytecode contained in the class files. But do be warned that the bytecode does not resemble the Java source code at all.
The definite source for learning about the Java bytecode and the Java Virtual Machine itself would be The Java Virtual Machine Specification, Second Edition. In particular, Chapter 6: The Java Virtual Machine Instruction Set has an index of all the bytecode instructions.
To view bytecode instruction of class files, use the javap -v command, the same way as if you run a java program, specifying classpath (if necessary) and the class name.
Example:
javap -v com.company.package.MainClass
About the bytecode instruction set,
Instruction Set Summary
Fernflower is an analytical decompiler, so it will decompile classes to a readable java code instead of bytecodes. It's much more usefull when you want to understand how code works.
If you have a class and no source code, but you have a bug, you can do one of two basic things:
Decompile, fix the bug and recreate the jar file. I have done this before, but sysadmins are leery about putting that into production.
Write unit tests for the class, determine what causes the bug, report the bug with the unit tests and wait for it to be fixed.
(2) is generally the one that sysadmins, in my experience, prefer.
If you go with (2) then, in the meantime, since you know what causes the bug, you can either not allow that input to go to the class, to prevent a problem, or be prepared to properly handle it when the error happens.
You can also use AspectJ to inject code into the problem class and change the behavior of the method without actually recompiling. I think this may be the preferable option, as you can change it for all code that may call the function, without worrying about teaching everyone about the problem.
If you learn to read the bytecode instructions, what will you do to solve the problem?
I have two question
1) What tools are available to view java byte code (preferably available
from the linux command line )
The javap tool (with the -c option) will disassemble a bytecode file. It runs from the command line, and is supplied as part of the Java SDK.
2) What are good references to get familiar with java byte code syntax
The javap tool uses the same syntax as is used in the JVM specification, and the JVM spec is naturally the definitive source. I also spotted "Inside the Java Virtual Machine" by Bill Venners. I've never read it, and it looks like it might be out of print.
The actual (textual) syntax is simple and self explanatory ... assuming that you have a reference that explains what the bytecodes do, and that you are moderately familiar with reading code at this level. But it is likely to be easier to read the output of a decompiler, even if the bytecodes has been fed through an obfuscator.
You might find the Eclipse Byte Code Outline plugin useful:
http://andrei.gmxhome.de/bytecode/index.html
I have not used it myself - just seen it mentioned in passing.

Self modifying code in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Have you ever created or encountered a self modifying code in Java?
If yes, then please post the link or simply post the code.
Ignoring the world of grief you could be causing yourself via self-modifying code(!), it seems to me there are 3 options:
use the inbuilt compiler support of Java 6 and write/recompile/reload classes
use the Apache BCEL bytecode manipulation library to write your class directly
make use of Java 6's inbuilt scripting support (or use Apache BSF) to write methods in your JVM scripting language of choice, and execute these
Of the three above, my initial choice (in the absence of requirements) would be to take a look at option 3. I suspect it's the least painful way to start. I've used all of the above - unfortunately I can't post links to client code.
You can write (Java) code that generates new classes (byte code) at runtime using a library like bcel. That's not quite the same as self-modifying code. I suspect self-modifying code is not something the JVM supports.
For an example of generating new code at runtime, have a look at the source code of clojure.
That should be difficult to realize. But you can create at runtime new classes and load them with a custom classloader. If you want to modify the code again, you have to reload the class.
From BCEL:
The Byte Code Engineering Library is intended to give users
a convenient possibility to analyze, create, and manipulate
(binary) Java class files (those ending with .class).
Classes are represented by objects which contain all the
symbolic information of the given class: methods, fields and
byte code instructions, in particular.
I see these options for this purpose:
Generate the java source code and compile it with the external javac or the internal compiler tools (can't remember the name). And as you are responsible for the naming, just include a version count in the class name to avoid class loading anomalies.
Use the built in JavaScript engine support
Some scenarios can be solved using java Proxys
Edit: I once created a Java 1.4 program which took business rules from an existing legacy database, generated java files (basically implementations of a Predicate interface) with a bunch of println() from them and used the command line javac to compile them.
As an undergrad I got to work on the JikesRVM. It is a JVM implemented (mostly) in Java. At runtime it will JIT compile itself! It's a really cool piece of technology.
You could always just use a dynamic language...

Categories

Resources