Results 1 to 10 of 10
- 04-02-2011, 04:14 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
JavaCompiler giving an error WITHOUT ide
I am writing a program, where i need to compile one java file from within another, on the fly.
the code is correct, and is working correctly under BlueJ. but when i export a jar file, or run it from the command-line, it gives me a null-pointer exception!! PLS HELP!!!
Java Code:JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); if(compiler.run(null, null, null, "Hello.java") {} else {}
Last edited by divs1210; 04-02-2011 at 04:18 PM.
- 04-02-2011, 09:26 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
As the getSystemJqavaCompiler() documentation says "Returns: the compiler provided with this platform or null if no compiler is provided".
Have a look for tools.jar in the lib directory (folder) of your JDK installation. This is where the compiler implementation is. You should include this jar file as part of the classpath when you run the program.
(Typically you set the classpath as part of the command line you use to compile or run the program - ie avoid setting or using a system variable)
- 04-02-2011, 09:34 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Java Code:C:\Documents and Settings\pbrockway2\Desktop>type CompilerEg.java import javax.tools.ToolProvider; import javax.tools.JavaCompiler; public class CompilerEg { public static void main(String[] args) { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); System.out.println(compiler); } } C:\Documents and Settings\pbrockway2\Desktop>javac -cp . CompilerEg.java C:\Documents and Settings\pbrockway2\Desktop>java -cp . CompilerEg null C:\Documents and Settings\pbrockway2\Desktop>java -cp .;"C:\Program Files \Java\jdk1.6.0_20\lib\tools.jar" CompilerEg com.sun.tools.javac.api.JavacTool@14ed9ff
- 04-03-2011, 04:52 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
i want it to be platform independent!!!
is there NO other way i could do this? can i search for the java/lib directory inside my program?
- 04-03-2011, 08:12 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Being "platform independent" is something of a marketing slogan. I mean the end user must have a computer, a monitor maybe, a jre (and one of the OS's which support it), and tools.jar or whatever other vendors' equivalent may be. So there are lots of preconditions that the platform must meet in order to run those lines of code.
Searching for tools.jar (assuming that's what you search for! The docs just say "the compiler provided with this platform") sounds like the sort of thing an installer program might do. (Guided by whoever is installing it.) Once found a launching batch file could be written that specifies the classpath.
- 04-04-2011, 08:24 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
how do the different java IDEs know where the java/lib folder is??
maybe i could use the same technique??? after all, java ides do the same thing, right?
- 04-06-2011, 06:44 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
I don't know, but I would guess the search local disk drives and/or ask the user when the program is installed.
java/lib need not be unique, and in general a computer will have multiple java runtimes. Eclispse, for instance, allows you to set which you want to use for a given project.
- 04-06-2011, 07:40 AM #8
Also realize that tools.jar is included with the JDK, not with the JRE.
db
- 04-08-2011, 10:13 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 16
- Rep Power
- 0
ah! finally, a solution. thanks, pbrockway2
and darryl.burke, thank you for the info. that means i'll have to supply tools.jar with my software, doesn't it?
- 04-09-2011, 01:04 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
I'm glad you've got it figured out.
For the simple programs I've written it has been possible to use a copy of tools.jar in some convenient place. (ie there haven't been more subtle dependencies) If you're worried by such things you might want to check out the legal side of things: whether you're *allowed* to redistribute tools.jar independent of the other bits of the JDK.
Similar Threads
-
JavaCompiler Help
By morris4019 in forum Advanced JavaReplies: 9Last Post: 03-06-2011, 07:08 AM -
Java code giving error
By alfarida in forum New To JavaReplies: 3Last Post: 11-10-2010, 07:11 PM -
MidletSample program giving java/lang/ClassFormatError: Bad version informatan error
By madhuchannama in forum Sun Java Wireless ToolkitReplies: 1Last Post: 07-23-2010, 02:42 PM -
Pls check why my code is giving a runtime error
By Beginner in forum New To JavaReplies: 3Last Post: 05-26-2010, 12:32 AM -
giving missing return statement error.due to withdraw method.
By qadeer37 in forum New To JavaReplies: 5Last Post: 01-16-2010, 11:14 PM
Bookmarks