Compile another java file from java file - java

Just out of curiosity. Can we compile & run a java file from another java program?
If so, can you send a reference to that knowledge source?

Take a look at Java Compiler Api and this little example.

If you have the java source code already in a file, then you can just call the java compiler. The java compiler is built-in to the JVM libraries as of version 1.6.
The interface is documented here.

Didn't read it thoroughly, but maybe this helps.

If you're using Java 6, the best way to do this is through the javax.tools.JavaCompiler interface.
If you're using an older version of Java, you must call javac directly using Runtime.exec(), then load the class data by subclassing ClassLoader and overriding findClass.

Yes, you can, but you need java compiler and not only java runtime. First you generate your source, save it and then use Dynamic class loading(tutorial http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html)

You can use javac (JDK is needed):
http://www.javaworld.com/javatips/jw-javatip131.html

You can do it by calling the cmd prompt or use the Main class from the Java Code.
I don't know remember well how is it, but I did it a long time ago.

Related

Using debugger in Java can I access Assembly code (OPCODE) and change it?

I have a java program loaded in the memory. Using a Java debugger at runtime, can I access the assembly code and change the next OPCODE and then rerun the program? Please let me know if this is feasible.
Thanks in advance!
I'm not sure about editing it but with the java compiler you can certainly generate it with something like
javac MyClass.java
javap -c MyClass > MyClass.bc
here is a good article to understand it from
http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
I imagine there is a way to change it then recompile with javap
edit
There are apparently some open source compilers for java byte code see http://en.wikipedia.org/wiki/Jasmin_%28Java_assembler%29
You can alter bytecode at runtime using a library such as javassist. Have a look:
http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/
Which debugger are you using? If you use Eclipse IDE you can alter the code even when the program is already running without the need of stopping it. This can only be done if no interfaces of the class are affected, though.
So the short answer to your question is: Yes, it can be done.
But it depends on your debugger implementation.

Support for package "com.ibm.misc" and its classes

I wanted to decode a Base64 string in my XPage for which I was using sun.misc.BASE64Decoder class. But according to Java developer should not write programs that call 'sun' packages. I was searching for an alternative when I stumbled on com.ibm.misc.BASE64Decoder. It worked for me with same results as sun.misc.BASE64Decoder. So I would like to know if it is okay for developers to use this package and its classes? Or is it to be avoided like 'sun' package?
Also I know that I can use Apache Commons for Base64 but I would like to minimize my dependency on external JARs.
With com.ibm.misc.BASE64Decoder you'll have exactly the same problem as with sun.misc.BASE64Decoder: it's an internal class which only exists in a specific JVM implementation, in this case IBM's JVM.
Note that there is no com.ibm.misc.BASE64Decoder in Oracle's JVM, so if you use this class, your program is not going to work on Oracle's JVM; it will fail with a NoClassDefFoundError.
You could use the method that mre refers to in his comment, which is in the class javax.xml.bind.DatatypeConverter - part of the JAXB API, which is part of the standard Java API (since Java SE 6).

Execute Java code 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.

Can some one tell me a way to find the native implementations of java methods?

Can some one tell me a way to find the native implementations of java methods
You can get the complete source for openjdk including the (c/c++) implementations of the native methods here: http://download.java.net/openjdk/jdk6/
(There are bundles for jdk 7 and jdk 8 too)
Native implementation of java function are done in JNI. If you have a class call org.abc.MyClass.java, its JNI implementation fora ny native function wil be in a file like org_abc_MyClass.c.
Dowload the jdk(link posted in previous answer) then use locate(to search name) and ack(a grep replacement to find stuff located inside the files, ex. ack --java "regex")

how can i call a java class in ruby

How can i call a java class in ruby?
If you use JRuby, you can
require 'java'
and then instantiate a Java class using eg
object = Java::package.package.ClassName.new
and then call methods using
object.method(parameter)
for more information, see Scripting Java libraries with JRuby
try this Java/Ruby Bridge:
Link
The Bridge itselfs enables you to load java classes into your ruby Code.
Can you wrap, or call, the Java class with another Java class with a main() method reading stdin or command-line arguments ? You can then spawn that as an executable from Ruby, write to stdin and read from stdout.
That may be the simplest answer, bearing in mind it's not the fastest mechanism, or in some cases the most practical. For some scenarios, however, it may be the most pragmatic.

Categories