Results 1 to 10 of 10
  1. #1
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default 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 05:18 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  4. #4
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  6. #6
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Also realize that tools.jar is included with the JDK, not with the JRE.

    db

  9. #9
    divs1210 is offline Member
    Join Date
    Apr 2011
    Posts
    16
    Rep Power
    0

    Default

    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?

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. JavaCompiler Help
    By morris4019 in forum Advanced Java
    Replies: 9
    Last Post: 03-06-2011, 08:08 AM
  2. Java code giving error
    By alfarida in forum New To Java
    Replies: 3
    Last Post: 11-10-2010, 08:11 PM
  3. Replies: 1
    Last Post: 07-23-2010, 03:42 PM
  4. Pls check why my code is giving a runtime error
    By Beginner in forum New To Java
    Replies: 3
    Last Post: 05-26-2010, 01:32 AM
  5. Replies: 5
    Last Post: 01-17-2010, 12:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •