I found this code:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if(compiler.run(null, null, null, fileName) != 0) {
System.err.println("Could not compile.");
System.exit(0);
}
However, this returns a NullPointerException
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at net.foxycorndog.foxy.compiler.Compiler.compile(Compiler.java:25)
at net.foxycorndog.foxy.compiler.Parser.parse(Parser.java:41)
at net.foxycorndog.foxy.Foxy$ActionHandler.actionPerformed(Foxy.java:99)
I read that the JRE does not include the ability to compile within a java program, but the JDK does.
I don't want a program that only works on a few computers that have had to manually set the path to the JDK library instead of JRE. With this in mind, is there any work around for this problem?
I would also like it to work over cross platform if possible.
There's no workaround to the fact that the JRE doesn't contain a compiler, and the JDK does. If you want to use the Java compiler, then you'll need to run your program with the JDK.
There are other ways to create executable Java code at runtime, however; there are various bytecode assemblers that let you build your code dynamically without use of a compiler. ASM is one that rocks hard; the Apache BCEL is another.
Related
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 ...
I am having a doubt.
My understanding is that jdk has [ jre + development tools (Java, javac, debugger etc.) + source code (src.zip) ].
Now working of java compiler is nothing to do with the running of class file.
If I am compiling a .java file then from where the java compiler is importing the package?
I could find the packages under jre.
If I do not opt to install jre while installing jdk, does that mean I will not be able to compile the java file having import statement?
Please help.
First, as a minor remark, a statement like
import java.util.List;
just introduces an abbreviation, allowing you to use the simple word List later in your code instead of the full class name java.util.List. So it's not so much the import statement itself, but the usage of a class like java.util.List that needs some explanation.
You understand correctly that, to compile your java file, the compiler needs some information about every class you use, and it typically finds this information in some jar file containing that class.
Now, where is this jar file containing the java.util.List class that the compiler reads? You're correct, it comes from the JRE, from the rt.jar that's part of the system classpath (the Java compiler itself is a java program that needs the basic classes itself, so wherever you successfully run javac, you always have an rt.jar available).
If your source code used a class from some other library, you'd have to specify that library on the javac command line, using an option like -cp.
Jdk = JRE + other tools like you mentioned.
When you are compiling your java file and you are using java inbuild library then it uses rt.jar to resolve dependency i.e import statements.
You can refer below link for the difference
What is the difference between JVM, JDK, JRE & OpenJDK?
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.
So, java has a built in library dedicated for compiling java source code into .class files, and it is in javax.tools. So, I was wondering how exactly you get it to work. I've read through the javadoc, and it gives some examples in there, but when I use those examples, I get errors.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
That is the example oracle gives in order to get an instance of the StandardJavaFileManager class from which you can do much more. However, I'm having some issues with the very first line of that code. When I attempt to do ToolProvider.getSystemJavaCompiler();, it always returns null. In the javadocs for that method, it says, "returns the compiler provided with this platform or null if no compiler is provided." But they never show any other way of getting an instance of a JavaCompiler. I've tried many other ways, such as using a ServiceLoader to find any reference of it that I could, but to no prevail. How might I go about getting this to work?
Chances are you're running Java from a JRE directory instead of a JDK directory - you need to run a version which "knows" where the Java compiler is.
So for example, on my Windows boxing, running a tiny test app like this:
import javax.tools.*;
class Test {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println(compiler);
}
}
The results are:
c:\Users\Jon\Test>"\Program Files\Java\jdk1.7.0_09"\bin\java Test
com.sun.tools.javac.api.JavacTool#1e0f2f6
c:\Users\Jon\Test>"\Program Files\Java\jre7\bin\java" Test
null
As you can see, it's fine when I specifically run the JDK version, but I get null when running the JRE version. Check how you're starting Java.
The compilation is often performed by directly invoking the javac compiler, which requires an installed Java Development Kit (JDK) or by calling com.sun.tools.javac.Main, which can be found in Sun's tools.jar. Sun's licensing allows tools.jar to be redistributed with the full Java Runtime Environment (JRE). Other ways to implement such dynamic capabilities include using an existing dynamic scripting language (such as JavaScript or Groovy) that integrates with the application's implementation language (see Resources) or writing a domain-specific language and associated language interpreter or compiler.
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