This discussion is archived
6 Replies Latest reply: Feb 28, 2010 9:43 AM by 843810 RSS

how do I add a jar file into the build path of the compiler?

843810 Newbie
Currently Being Moderated
Hey,

I'm trying to import a jar file into the build path of the compilation process, but it does not find the packages or the classes that are in it.
I think I don't add it right...
          ArrayList<String> options=new ArrayList<String>();
          options.add("-d");
          options.add(targetDirectory);
          options.add("-classpath");
          for(String str:includeDirectory)
               options.add(str);
          if (!compiler.getTask(writer, fileManager, diagnostics, options, classes, compilationUnits).call());
                ....
and I've tried this way:
     public void setTargetDirectory(String targetDirectory) {
          this.targetDirectory = "-d " + targetDirectory;
     }
     
     private void compile(Iterable<? extends JavaFileObject> compilationUnits) throws Exception {
          ArrayList<String> options = new ArrayList<String>();
          options.add(targetDirectory);
          String classPath="-cp ";// tried this also with "-classpath"
          for (String str : includeDirectory)
               classPath+=str+":";
          options.add(classPath);
     if (!compiler.getTask(writer, fileManager, diagnostics, options, classes, compilationUnits).call())
               ;
          // throw new Exception("Compilation Error");
     }
Thanks in advance,
Adam.

Edited by: Adam-Z. on Feb 24, 2010 5:41 AM

Edited by: Adam-Z. on Feb 24, 2010 5:42 AM
  • 1. Re: how do I add a jar file into the build path of the compiler?
    843810 Newbie
    Currently Being Moderated
    well, I found out that this way the destination directory works, and that the structure of the build path should be as implemented:
         public void setTargetDirectory(String targetDirectory) {
              this.targetDirectory = targetDirectory;
         }
         
         private void compile(Iterable<? extends JavaFileObject> compilationUnits) throws Exception {
              ArrayList<String> options = new ArrayList<String>();
              options.add("-d");
              options.add(targetDirectory);
              options.add("-classpath");
              String classPath=System.getProperty("java.class.path");
              for (String str : includeDirectory)
                    classPath+=";"+str;
              options.add(classPath);
    
         if (!compiler.getTask(writer, fileManager, diagnostics, options, classes, compilationUnits).call())     
               throw new Exception("Compilation Error");
         }
    this is my classpath String value:
    D:\%Important Documents\WorkSpaces\PacMan\ApplicationManager\bin;D:\%Important Documents\WorkSpaces\PacMan\J2SeBasic\bin;D:\%Important Documents\WorkSpaces\PacMan\Pac-Man Revenge\bin;E:\WTK2.5.2\lib\midpapi21.jar;E:\WTK2.5.2\lib\cldcapi11.jar;E:\WTK2.5.2\lib\wma20.jar;E:\WTK2.5.2\lib\mmapi.jar;E:\WTK2.5.2\lib\j2me-ws.jar;E:\WTK2.5.2\lib\j2me-xmlrpc.jar;E:\WTK2.5.2\lib\jsr75.jar;E:\WTK2.5.2\lib\jsr082.jar;E:\WTK2.5.2\lib\jsr184.jar;E:\WTK2.5.2\lib\jsr179.jar;E:\WTK2.5.2\lib\satsa-apdu.jar;E:\WTK2.5.2\lib\satsa-jcrmi.jar;E:\WTK2.5.2\lib\satsa-pki.jar;E:\WTK2.5.2\lib\satsa-crypto.jar;E:\WTK2.5.2\lib\jsr211.jar;E:\WTK2.5.2\lib\jsr238.jar;E:\WTK2.5.2\lib\jsr229.jar;E:\WTK2.5.2\lib\jsr180.jar;E:\WTK2.5.2\lib\jsr234.jar;E:\WTK2.5.2\lib\jsr226.jar;E:\WTK2.5.2\lib\jsr239.jar;D:\%Important Documents\WorkSpaces\PacMan\ApplicationManager\Plug-in\Data Chunk Designer.jar
    I keep getting this error:

    Error details: D:\%Important Documents\WorkSpaces\PacMan\J2Me Tools Generator\Plugins\imageCropper_Editor\ImageCroper_Editor.java:3: package j2MeDataChunkGenerator_Plugin does not exist

    this directory is found in the last jar on the classpath list parameter. the compiling application does not have a reference to the jar in the class loader,I only added this to the compilation classpath.

    any idea why: package j2MeDataChunkGenerator_Plugin does not exist

    Thanks in advance,
    Adam.
  • 2. Re: how do I add a jar file into the build path of the compiler?
    608410 Newbie
    Currently Being Moderated
    Adam,

    Q: Are there *.class files in that directory in that jar file? (the compiler doesn't ( [can't|http://java.sun.com/javase/6/docs/api/index.html?javax/tools/JavaFileManager.html] )) look for directories, it can just [look for specific files|http://java.sun.com/javase/6/docs/api/javax/tools/JavaFileManager.html#getJavaFileForInput(javax.tools.JavaFileManager.Location,%20java.lang.String,%20javax.tools.JavaFileObject.Kind)] , and scan to get a list of all files matching certain criteria. So if there are no class files, it will say the package doesn't exist, even if there is a directory, possibly containing other files.

    Also, your code will only work on windows because other platforms use a different path separator. You should use java.io.File.pathSeparator not explicit ';" when building your classpath. (this is unrelated to your problem, but you should correct it)

    Q: Is that error in your post formatted by your own diagnostics? (we could possibly help you better if we didn't have to guess!!)

    Q: Is line 3 of ImageCroper_Editor.java (sic) an import statement? (we could possibly help you better if we didn't have to guess!!)

    Also you don't show us what the variable includeDirectory is in terms of type, and contents, that might be helpful. (we could possibly help you better if we didn't have to guess!!)
    this directory is found in the last jar on the classpath list parameter.
    Q: Have you proven this?

    Q: Is the compiler finding other classes (in other packages) in that same jar file?


    Bruce
  • 3. Re: how do I add a jar file into the build path of the compiler?
    843810 Newbie
    Currently Being Moderated
    Thank you for your reply,
    Q: Are there .class files in that directory in that jar file? (the compiler doesn't ( can't )) look for directories, it can just look for specific files , and scan to get a list of all files matching certain criteria. So if there are no class files, it will say the package doesn't exist, even if there is a directory, possibly containing other files.
    yes there are class files in the jar, the tree structure:
    j2MeDataChunkGenerator_Plugin\(lots of class files)
    META-INF\manifest.mf

    and thats it.

    , your code will only work on windows because other platforms use a different path separator. You should use java.io.File.pathSeparator not explicit ';" when building your classpath. (this is unrelated to your problem, but you should correct it)
    will do, thanks.

    Q: Is that error in your post formatted by your own diagnostics? (we could possibly help you better if we didn't have to guess!!)
    I would not post my own error code, this text is generated by the compiler diagnostic.
    {code}
         System.err.println(" Error details: " + diagnostic.getMessage(null));
    {code}
    Q: Is line 3 of ImageCroper_Editor.java (sic) an import statement? (we could possibly help you better if we didn't have to guess!!)
    it is an import error... didn't the error message stated that it is an import problem? wired, I'm sure before it did. anyway it is an import error.
    Also you don't show us what the variable includeDirectory is in terms of type, and contents, that might be helpful. (we could possibly help you better if we didn't have to guess!!)
    It has only one String object: "D:\%Important Documents\WorkSpaces\PacMan\ApplicationManager\Plug-in\Data Chunk Designer.jar"
    the last file on the classpath list.
    Q: Have you proven this?
    that i did post, in this long line of text.
    Q: Is the compiler finding other classes (in other packages) in that same jar file?
    No. all the class files are in the jar, they all have entries that start with "j2MeDataChunkGenerator_Plugin\*.class", and since I get 47 errors I guess it does not load any other class.


    thank you for you comments, the problem with having these errors, is that I can't even get a piece of information where this error is coming from, only that it is an import loading error package not found, what does that mean? that the jar was not loaded in compilation(no error about this), that the jar is corrupted(no error about this), that the path is incorrect(it is correct I made sure), that there is no such package in the jar(There is), that the compiler does not load the package(does it even do that?), really I can't even guess why this happens, I've been at this on and of all day today, really annoying.

    Thanks,
    Adam.
  • 4. Re: how do I add a jar file into the build path of the compiler?
    843810 Newbie
    Currently Being Moderated
    I tried a jar that I compiled with Eclipse compiler, and I probably didn't build my jar correctly...
    thanks for the trouble.

    Adam.

    Edited by: Adam-Z. on Feb 24, 2010 2:13 PM
  • 5. Re: how do I add a jar file into the build path of the compiler?
    608410 Newbie
    Currently Being Moderated
    Adam-Z. wrote:
    I tried a jar that I compiled with Eclipse compiler, and I probably didn't build my jar correctly...
    thanks for the trouble.

    Adam.

    Edited by: Adam-Z. on Feb 24, 2010 2:13 PM
    Does this mean you have found the problem?

    if yes, then what was the problem? (I am guessing (from preceding post, and original problem) that the actual jar file had no class files in it - is this correct?)

    Do you need further help?

    Bruce
  • 6. Re: how do I add a jar file into the build path of the compiler?
    843810 Newbie
    Currently Being Moderated
    Well, the problem was that when I generated my own Jar file, I had the entries path to be package\\className.class, the winrar sees it no matter the separator '/', '\\', but when my class loader was loading from the jar it was set to look for package/className.class, as the eclipse jar entries are.

    No, thank you, no further help is necessary.

    Thanks,
    Adam.