Generating, compiling and using Java code at run time? - java

I have a scenario where I have to generate Java code when my application is running.
Can compile and run than code from my application( as a separate application).
Is it possible to compile and then use it from the same application. Cant think of any possibility

Check out Create dynamic applications with javax.tools. Second time I've referenced this today--I swear I don't work for them.

You can use an instance of JavaCompiler:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Follow the link for the an example on how to use it.

using the ProcessBuilder or Runtime.exec() you can run any commandline app from your java code
this includes the javac compiler and java as a separate process

Related

how to avoid using javac -cp

I have a Java class which uses a .jar file.
Every time that I want to compile this class, I have to do something like javac -cp ".:myJar.jar" myClass.java and every time that I want to execute it, I have to do the similar thing but with java instead of javac.
Is there a way to avoid doing this?
I know that I could put this jar file into my class path but I don't want to do that. I don't neither want to do a maven projet.
There is nothing preventing you from using an IDE, taking advantage of all its super useful features when developing the program, and then submit just the source code and associated jars to the professor.
Advantages of using IDE that your professor don't need:
Syntax color-coded editors with auto-complete.
Built-in display of javadoc, so you know what all the built-in Java methods do, and how they work.
Instant high-lighting of syntax errors.
Never having to compile the code, since IDE always keeps the code compiled.
Easy single-button execution of the program when you want to run it.
Debugger. Very important feature when your code is not working as you intended.
... more ...

Generate a Java class from Clojurescript using Rhino

I'd like to do the following: I have a simple function written in Clojure/ClojureScript:
(defn add
[a b]
(+ a b))
I want to wrap this function into a Java class and put it in a jar so that I can access it in an existing Java/Android project. My previous approach was to use gen-class and create an uberjar. This however leads to some problems.
As an alternative approach I considered compiling the function using ClojureScript (a solution also suggested by Sam Beran).
So far I understand how to:
compile javascript files into Java classes
put them in a .jar
Compile ClojureScript
I'm not struggling to get the ClojureScript output into a format that can be passed on to the Rhino compiler.
Any thoughts?
IMPORTANT NOTE: I do not want to create a class with a main function as is done here!
One general way of going about this would be to:
At build time:
run the ClojureScript compiler to generate a JavaScript file
put the JavaScript file into the resource directory.
compile the Java class that uses it
make a jar file (an Uberjar or a normal Unterjar)
this Java class should:
make a java class that on initialization start up Rhino
on instantiation runs the javascript from the resource.
On a desktop of server you may not get the same awesome startup times My. Beran reported on android because processes on android have the advantage of starting life with a warmed up runtime with Rhino ready to go from the moment they start (they inherit it from the Zygote process)

Compiling a Java Project using Compiler API

I'm trying to compile a whole java project dynamically using the Compiler API. My initial thoughts of achieving this is to first know how to compile it in one-line using command line, then apply the same principle/parameters on the compiler object. Problem is, I never did, err, compiled using CLI. (Disadvantage of using an IDE? Haha)
So, am I on the right track? Moreover, can the project compilation achieved in one line execution? I'm having a hard time figuring this out because of the fact that it's a project, it contains packages galore.
So you either want to learn javac or Java Compiler API?
If you want CLI compilation look at javac (Linux | windows).
Alternatively for API, then programmatic use of Java's compiler API will definitely require more that one line, this will get you the compiler:
JavaCompilercompiler =ToolProvider.getSystemJavaCompiler();
Then you'll still need to load classes, write out byte code, and possibly package as a JAR.
You should use StandardJavaFileManager as you've probably many classes to manage, there's an example in the top of the JavaCompiler javadoc, but search for StandardJavaFileManager+JavaCompiler+example to find clearer examples like this blog post.

How to create a method out of the text file?

I have a text (.txt) file that contains Java code! I want to create a method that includes this Java code and then call that method through the program.
Can anybody suggest a way to do this?
let consider this example what it does actually load the source code, compile and execute the java code by simpler program by using JavaCompiler API.
Use the JavaCompiler. It can compile code from a String, so I'm sure it could handle code from a text file.
Do you think instead of putting it in the main method I can put it in for example test method and call method like this?
Put it wherever you like. E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
this program need tools.jar but jre 7 doesnt have this!!
Did you try reading the documentation that is provided for the STBC? Notably:
System Requirements
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.
Or shorter, no JRE will have a JavaCompiler, only JDKs have them.
Change the .txt file to a .java file,
add it to your java project
Compile the code
Execute the methods
Load the file in through standard java IO and then have Groovy evaluate it for you:
http://groovy.codehaus.org/Embedding+Groovy
it's something like quine:
http://www.nyx.org/%7Egthompso/quine.htm

Java compiled with gcj using javax.comm api. Possible?

I have a java program that I'm required to compile into a Linux native program using gcj-4.3. This program requires serial port access. The javax.comm api provides serial port access but I'm not sure how to get my compiled java program to use it.
The target box has Java installed, but of course my compiled program isn't running in the JRE...so I'm not exactly sure how I can link in the comm.jar file or how that file can find the .properties file it requires.
I wonder if I can just compile the comm.jar allong with my .jar file and link the two object files together. Can my code then reference the classes in comm.jar?
Thanks in advance for your help!
I'm not an GCJ expert but I have some suggestions (I'm not providing the syntax, this will require some exploration that I didn't perform):
first, I think that you'll have to compile comm.jar into a (shared) library,
then, you'll have to link your code against the library,
finally, use the GCJ_PROPERTIES environment variable to pass properties to the program at invocation time.
The following pointers might be helpful to implement this:
GCJ---The GNU Compiler for Java (great resources IMO, covers all the steps mentioned above)
GCJ – Getting Started (more an intro but still nice)
Compile ActiveMQ with GCJ (more use cases but I don't think they apply here)
And of course, the official documentation :)

Categories