Results 1 to 10 of 10
  1. #1
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default JavaCompiler Help

    Hi everyone,

    I am attempting to create a small program which will help a Java instructor grade a particular assignment. If anyone has ever built a HuffmanTree Textfile encoder you are familiar with the assignment. But to the point. All students turn in a file named HuffmanTree.java. If all are put in the same directory, allowing windows to automatically rename any duplicates they will be named HuffmanTree.java, HuffmanTree (2).java,..., etc. I am designing a simple gui that will allow the instructor to browse and select all files to be tested. Each file, one by one, will be renamed and compiled, then tested, output written to the display, and then .class file deleted.

    My question is this. The above seems to work IF my software is ran from within an IDE (i'm using netbeans). More specifically I can obtain a compiler instance from ToolProvider.getSystemJavaCompiler. But I cannot seem to get it working properly from a jar file.

    My intent is to simply give the instructor the jar file for which she can use, but I'm running in to trouble because outside an IDE, getSystemJavaCompiler seems to return null every time.

    Any ideas on this subject would be great.

    Thanks

  2. #2
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    For a small test bit of code here is the following edited a bit from Java Tutorials....

    Java Code:
    import javax.tools.JavaCompiler;
    import javax.tools.ToolProvider;
    
    public class SimpleCompileTest {
    	public static void main(String[] args) {
                JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    
                if (compiler==null)
                    System.out.println("Compiler was null");
                else
                    System.out.println("Compiler NOT null");
    	}
    }

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I am having exactly the opposite experience -- the code above outputs null if I run it in my IDE (Eclipse), and not null if I run a Jar file I've created.

  4. #4
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    weird.... hmm... I am reading some thing regarding the JDK being installed and not just the JRE, which is fine, I have both, but, I had hopes that this was possible without the JDK being installed. That way if the instructor happened to have this on a flash drive and used it on a computer that simply had the JRE installed the program would work.

    Can I ask, have you manually setup at any time, any environment variables which have to do with the compiler?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    ok, I have that as well, but I do not belive that would have any bearing when it comes to the compiler. So i am still back at square 1. If i find anything out I will post it.

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

    Default

    I get null in Eclipse AND running the app with "java -cp . SimpleCompileTest" AND creating and running a jar.

    But I do have success with

    Java Code:
    >java -cp .;"C:\Program Files\Java\jdk1.6.0_20\lib\tools.jar" SimpleCompileTest

    The point is that tools.jar must be on the classpath. Now as far as Eclipse is concerned tools.jar may or may not be on the classpath depending on how you have things set up. Likewise the location may or may not become part of what is generated in the manifest of the jar you create. (Foobarable could check the manifest of the jar file to see if this is, in fact, the case.)

    I don't know if the entire JDK must be present (I suspect not) - but tools.jar is where the tools are (!) so at least this file must be on the pendrive or whatever for the compilation to take place.

  8. #8
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    I wonder if I could simply include a copy of tools.jar with my program inside the jar file.

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

    Default

    Don't do that.

    The usual procedure is to copy tools.jar (providing that is all that's needed) alongside your jar and have the manifest of your jar archive reference tools.jar as part of the classpath. See Adding Classes to the JAR File's Classpath in Oracle's Tutorial.

    As well as legal reasons it makes more sense to do things this way: the end user can replace tools.jar with an updated version if one becomes available. (fixing bugs or whatever).

  10. #10
    morris4019 is offline Member
    Join Date
    Nov 2010
    Location
    Federal Way, WA
    Posts
    18
    Rep Power
    0

    Default

    That did the trick. I downloaded a copy of Tools.jar and modified the "build-jar" file inside netbeans to add "Class-Path: Tools.jar". I then tested the code without Tools.jar in the same folder as the main .jar file and it did not work (as expected). Then i copied the Tools.jar file to the same directory as the main .jar file and it worked properly.

    Thanks for all your help!

Posting Permissions

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