I have the following weird requirement.
I am given:
toString
and obtains an array of strings. To this array, the method applies a function foo
. The function foo
takes as input a String []
type and outputs String
. The methods return what foo
returns.
foo
's code is given inside a Java object and is to be accessed as a black-box. The info in 1. and 2. can be in a text or XML file. For this purpose, we can consider it to be available inside a Java object in whatever way we choose.
The task is to create a .class
file (i.e., bytecode) that implements those methods and can be run on the JVM.
I think this assembler library would be one way to do it. Can anyone suggest an easier way?
[EDIT:] I can think of one other way: first generate the .java
file and then compile it to get the .class
file.
[The context:] I have to do this for several hundreds of methods. I want the shortcut so I can automate my job rather than manually write the code.
The . class file is the byte-code. It is the result of compiling java source code (text) into the intermediate format, byte-code. The byte-code is then interpreted by the JVM and compiled into a language understandable by your CPU.
To create a new Java class or type, follow these steps: In the Project window, right-click a Java file or folder, and select New > Java Class. Alternatively, select a Java file or folder in the Project window, or click in a Java file in the Code Editor. Then select File > New > Java Class.
Byte Code is automatically created in the same directory as . py file, when a module of python is imported for the first time, or when the source is more recent than the current compiled file.
class" file is created as a result of successful compilation by the Java compiler from the ". java" file. Each class in the . java file is compiled into a separate class file if the ".
You could generate the required program code in Java syntax and turn it into a class file using the compiler. It's possible to instantiate javac
at run time and pass it a byte array instead of the location of the source file. This is probably the easiest for other programmers to maintain.
If you want to generate byte code directly, asm is the library most commonly used.
Here is a list of Open Source ByteCode Libraries: http://java-source.net/open-source/bytecode-libraries
Have a look at Javassist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With