Include an in-memory jar file for JSR-199 compilation

I want to compile a source file in memory, which requires a jar file that is also represented in memory. I used the JavaSourceFromString class recommended in the documentation for the class JavaCompiler and in a demo I found online that shows how to compile sources represented as String in memory. To represent the jar file, I used a similar trick to JavaSourceFromString:
public class JarJavaFileObjectFromByteArray extends SimpleJavaFileObject {
   * The contents of this jar file.
  private final byte[] contents;
   * Constructs a new JarJavaFileObjectFromByteArray given the name and binary
   * contents of a jar file.
   * @param name the name of this jar file
   * @param contents the contents of this jar file
  public JarJavaFileObjectFromByteArray(String name, byte[] contents) {
    super(newURI(name), Kind.OTHER);
    this.contents = contents;
  ... // code not shown ensures that the URI returned from newURI is of the form,
      // for instance, bytes:///MathConstants.jar, if the name of the jar file is MathConstants.jar
  @Override
  public InputStream openInputStream() throws IOException {
    return new ByteArrayInputStream(contents);
}However, I do not know how to alert the compiler that this jar file should be on the classpath. I tried this:
List<String> options = Arrays.asList(" -classpath bytes:///MathConstants.jar ");
CompilationTask task = compiler.getTask(null, fileManager, null, options, null, compilationUnits);but I get the following error when calling getTask:
java.lang.IllegalArgumentException: invalid flag: -classpath bytes:///MathConstants.jarI assume there is some way to tell the compiler, "look at the MathConstants.jar file that I am storing in memory when searching the classpath", but I do not know how to do this. I assumed that the options parameter for getTask represents command-line flags that would be passed to the compiler if this were happening on the command line (such as "-cp .", which also does not work), but perhaps this assumption is wrong.

Hi Bruce,
I have a question regarding loading a jar file by the compiler to dynamically compile with a source file. I hope you can probably offer me an idea on what has been missing or wrong with the source codes I have written for my application.
I am using Eclipse compiler to dynamically compile a class. In the class, I want it to make a reference to a jar file for compilation dynamically.
Here is the source of a test class I wrote:
import javax.servlet.http.HttpServlet;
class MyServlet extends HttpServlet {
}The import statement refers to the class javax.servlet.http.HttpServlet from the jar file C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\lib\\servlet-api.jar placed in the file system.
In the method called compileClass (shown below), I used the path of the jar file to add to the option -classpath as you suggested.
     private static CompileClassResult compileClass(Writer out, String className, String classSource) {
          try {
               JavaCompiler javac = new EclipseCompiler();
               StandardJavaFileManager sjfm = javac.getStandardFileManager(null, null, null);
               SpecialClassLoader scl = new SpecialClassLoader();
               SpecialJavaFileManager fileManager = new SpecialJavaFileManager(sjfm, scl);
               List<String> options = new ArrayList<String>();
               options.addAll(Arrays.asList("-classpath", "C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\lib\\servlet-api.jar"));
               List<MemorySource> compilationUnits = Arrays.asList(new MemorySource(className, classSource));
               DiagnosticListener<JavaFileObject> diagnosticListener = null;
               Iterable<String> classes = null;
               if (out == null) {
                    out = new PrintWriter(System.err);
               JavaCompiler.CompilationTask compile = javac.getTask(out, fileManager, diagnosticListener, options, classes, compilationUnits);
               boolean res = compile.call();
               if (res) {
                    //Need to modify the api to return an array of two elements - one classes and other bytecodes for all classes in the same class file.
                    return CompileClassResult.newInstance(scl.findClasses(), scl.findByteCodes());
          } catch (Exception e) {
               e.printStackTrace();               
          return null;
     }I also extended the class ForwardingJavaFileManager as you suggested and have it delegated to the StandardJavaFileManager sent to the compiler mentioned in the method compileClass above. The extended class (called SpecialJavaFileManager) is as follows:
public class SpecialJavaFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {
     private SpecialClassLoader xcl;
     public SpecialJavaFileManager(StandardJavaFileManager sjfm, SpecialClassLoader xcl) {
          super(sjfm);
          System.out.println("SpecialJavaFileManager");
          this.xcl = xcl;
     public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
          System.out.println("getJavaFileForOutput");
          MemoryByteCode mbc = new MemoryByteCode(name);
          xcl.addClass(name, mbc);
          return mbc;
     public Iterable<JavaFileObject> list(JavaFileManager.Location loc, String pkg, Set kinds, boolean recurse) throws IOException {
          System.out.println("list ");
        List<JavaFileObject> result = new ArrayList<JavaFileObject>();
        for (JavaFileObject f : super.list(loc, pkg, kinds, recurse)) {
             System.out.println(f);
            result.add(f);
          return result;
}I run the application and the result shows that it didn't load the jar file into the memory as expected. From the output (below) I got, it doesn't seem to invoke the method list(...) in the class SpecialJavaFileManager.
SpecialJavaFileManager
1. ERROR in \MyServlet.java (at line 1)
     import javax.servlet.http.*;
            ^^^^^^^^^^^^^
The import javax.servlet cannot be resolved
2. ERROR in \MyServlet.java (at line 3)
     class MyServlet extends HttpServlet {
                             ^^^^^^^^^^^
HttpServlet cannot be resolved to a typeWould you please let me know what has possibly be missing or wrong?
Thanks.
Jonathan
Edited by: jonathanlam on Aug 10, 2009 6:47 PM

Similar Messages

  • Can I include dlls in a jar file for deployment?

    I have a set of dll's I need to deploy (some kind of database driver needed along with my java application) in a client/server situation. Instead of copying these dll's to the client's system32 folder on windows, can I simply include these dll's in the jar file containing my java applications?

    No it was just to make everything cleaner. But if I have to take them outside of the jar file, then like you say there's no benefit in putting them in there in the first place.
    As for having the dll's in the same directory or a sub directory, how can I load them then? Using the function System.loadLibrary?

  • Where is the JMS jar file for CEP?

    I cannot find in any jar finder the JMS jar file for CEP.
    I mean the jar file which includes com.bea.wlevs.adapters.jms.api.InboundMessageConverter, com.bea.wlevs.adapters.jms.api.OutboundMessageConverter, etc.
    Does anybody know the link?
    Thanks!

    Thanks, but it is not there.
    I only have the following:
    com.bea.core.jsmpool_1.7.0.0.jar
    javax.jms_1.1.1.jar
    org.springframework.spring-jms_2.5.6.jar
    And if I cannot find it there, where could it be?
    Thanks!

  • Jar file for com.bea.xbean.util.XsTypeConverter

    I am using weblogic 10.3.5 I used clientgen to generate java classes for a webservice and a few classes are using the class XsTypeConverter.
    I have imported weblogic.jar but it says package does not exist com.bea.xbean.util.XsTypeConverter.
    Where can i find the jar file for this under weblogic?
    Thanks,

    Thank you so much for the reply.I have included all the jars you suggested.I could not find wlfulclient.jar in my weblogic home but there was
    wlclient.jar so I included that but face the same error.
    I used weblogic 10.3.5 clientgen to geberate the java classes then why is it that the classes it uses cannot be found in the same version.
    where jar will have the file com.bea.xbean.util.XsTypeConverter.
    I have searched a lot on internet but not able to find anything.Please help

  • Including a DLL in JAR file (Comm API)

    Writing a little app that interfaces with serial port device using the communications API and I'd like to bundle it into an executable JAR. JBuilder will create the JAR for me fine, but the issue is with the win32com.dll file thats part of the comms API. I need to include this, I don't think its possible to include as part of the executable, but otherwise the DLL needs to be in the windows\system folder. Could I include the dll as part of the JAR archive and then extract it upon runtime? If so how would I go about this?
    Any help much appreciated.

    There is another option. Native libraries need to reside within the java.library.path location. As it turns out, if you, at runtime, grab this property and ADD your own location to it, then set it back in to the System.setProperty() call, it will NOT work. The only way to do it is the -Djava.library.path value that tjmaven suggested.
    However, if you read up on how a native library is found, it uses the ClassLoader.loadLibrary() which calls the protected findLibrary() method. You can therefore extend ClassLoader to find a path yourself.
    One way is to extend URLClassLoader since it has the protection/security stuff AND the ability to look in .jar files for you. Your custom loader is created by a "launcher" class in your .jar file. First, your luancher uses getClass().getProtectionDomain().getCodeSource().getLocation() to get the absolute location of the .jar file its executing within. From there, you open the jar and look for your native library, extract it using the JAR api to a location of your choosing. Now, you instantiate the custom classloader and using this custom loader instance you load the rest of your classes. The main .jar file contains two classes, the launcher and the custom classloader, and contains the native library if you wish to do it this way. The custom classloader, you specify a URL[] of "classpath" entries. You bundle up the rest of your source in a second .jar file that you pass to the URL[] when creating the custom classloader. It will find all your classes in the 2nd jar no sweat. You can even "reload" these classes on the fly by recreating the custom classloader instance if you so wish to add a sort of "reload" feature.
    Now, in your custom classloader, you override the findLibrary() and return the SAME STRING PATH that you used to unjar your native files to. This way, when the JVM asks your custom loader to load the library at some path, you return the right path to find the native files in.
    I haven't verified that this works just yet, but I will soon. Primarily, my plugin engine over at www.platonos.org will support the ability to place native libraries in plugins, allowing you to effectively wrap a native library use as a plugin. Not yet working, but we'll soon have it in place.

  • MBeanMaker is not creating Schema folder in JAR file for custom authenticat

    I have created a custom authentication provider.I am using following code of build.xml to generate jar file for custom authentication provider
    <target name="build.mdf" depends="copyConfigFiles">
    <java classname="weblogic.management.commo.WebLogicMBeanMaker" fork="true" failonerror="true">
    <jvmarg line="-Dfiles=./build/mili -DMDF=./build/mili/MILIAuthenticator.xml -DcreateStubs=true"/>
    <classpath>
    <pathelement path="${java.class.path}"/>
    <pathelement location="./build/mili"/>
    <pathelement location="./lib/weblogic.jar"/>
    <pathelement location="./lib/wlManagement.jar"/>
    <pathelement location="./lib/jcifs-1.2.0.jar"/>
    </classpath>
    </java>
    </target>
    <target name="copySrcFiles" depends="build.mdf">
    <copy todir="./build/mili" flatten="true" overwrite="true">
    <fileset dir="./src/java">
    <include name="**/MILI*.java"/>
    </fileset>
    </copy>
    </target>
    <target name="SecurityProviderJar" depends="copySrcFiles">
    <java classname="weblogic.management.commo.WebLogicMBeanMaker" fork="true" failonerror="true">
    <jvmarg line="-Dfiles=./build/mili -DMJF=./build/MiliSecurityProviders.jar"/>
    <classpath>
    <pathelement path="${java.class.path}"/>
    <pathelement location="./build"/>
    <pathelement location="./lib/wlManagement.jar"/>
    <pathelement location="./lib/weblogic.jar"/>
    <pathelement location="./lib/jcifs-1.2.0.jar"/>
    </classpath>
    </java>
    </target>
    Build.xml is running fine without giving any error,but it is not generating schema folder in the jar file.
    beacause of that my custom authentication provider is not comming in admin console.
    Kindly help me to resolve this.

    Hi Ramesh,
    Try out the following
    1) Copy mediator.jar from SOA_HOME>/soa/modules/oracle.soa.mediator_11.1.1 to <MW_HOME>//soa/modules/oracle.soa.ext_11.1.1/classes
    2) Goto <MW_HOME>//soa/modules/oracle.soa.ext_11.1.1/classes and run the following command:
    a. jar -xvf mediator.jar
    3) Now remove the jar file, i.e., mediator.jar from classes folder.
    4) Restart the server.
    Regards,
    Srijith

  • Help! How to create Jar file for a packaged class?

    Hi!
    I am new in jar complexities. I made a swing frame that just prompts a JOptionPane when executed. I accomplished the same using jar without packaging my class SwingTest.
    But when i package it, it doesn't run. Can any one tell me how to make jar file of packaged classes and include images in the jar files too!
    Really Thanx!

    Call the Jar from the commandline to see the exceptions thrown.
    java -jar <jarFileName> <className>

  • How to make .jar file for a multiple class program

    I have a code for a chat room that contains two folders , one for Server and one for Client
    What I need is to create a jar file for the server and a jar file for the clients
    but here the first problem , I have a file that named (SocketMessengerConstants.class) that is located outside the two folders and is being read by the two projects .
    I mean the server uses it while running and the client uses it too .
    the second problem is : I don't know how to create a jar file at all !
    please tell me how to do it in detail
    Thank you

    ChuckBing wrote:[Packaging Programs in JAR Files|http://java.sun.com/docs/books/tutorial/deployment/jar/index.html]
    I tried to read this tutorials before I come here but I couldn't understand it
    can you please tell me a specific steps to follow instead of the explaining .. because I understand the explanation but I can't apply it

  • How to create a .jar file for the BPEL project

    hi all,
    how can i create a .jar file for my BPEL project, i am trying to deploy the project but getting an error :
    "Error: [Error ORABPEL-10902]: compilation failed [Description]: in "C:\OraBPELPM\integration\jdev\jdev\mywork\Workspace_BPELDemo\BPEL_Report\BPEL_Report.bpel", XML parsing failed because "". [Potential fix]: n/a. "
    and when doubel cliking on this error Jdev take me to the beginning of the ".bpel" file.
    I 'm really stuck in here i am not able to deploy the process!
    Thanks,
    Rana

    Rana:
    If you haven't already, I would post this question in the BPEL forum. I would expect a much better turnaround there.
    Johnny Lee

  • Virus scan failed Error on deploying a web application having a jar file for calling the applet on the jsp page

    Hi,
    I have an applet application that i want to deploy on the Oracle cloud.
    So i have created a jar file for the applet application and i am using this jar to call the applet on a jsp page.
    But when i am delpoying my application on the java cloud, its giving me the below error:
    2014-10-28 03:16:41 CDT: Starting action "Virus Scan"
    2014-10-28 03:16:41 CDT: Virus Scan started
    2014-10-28 03:16:49 CDT: ----------------------------------------------------------------------
    2014-10-28 03:16:49 CDT: File Scanned: "Application7.ear".
    2014-10-28 03:16:49 CDT: File Size: "106698122".
    2014-10-28 03:16:49 CDT: File Status: "INFECTED".
    2014-10-28 03:16:49 CDT: ----------------------------------------------------------------------
    2014-10-28 03:16:49 CDT: Virus scan failed.
    2014-10-28 03:16:49 CDT: "Virus Scan" complete: status FAILED
    Can't we deploy any application having applet or swing component's onto the cloud?
    Or do we need to request for any extra permissions for the same?
    Thanks,
    Manoj

    I don't see applets mentioned in the supported features nor in the unsupported features so not sure if they are supported you would likely need to contact the operations team to confirm.
    Jani Rautiainen
    Fusion Applications Developer Relations
    https://blogs.oracle.com/fadevrel/

  • Jar files for using BRM API's

    Hello,
    I am not able to locate the Jar files for using BRM API's. I have tried searching the forum and did not find the name or location from where I can get them. If they are available on the server I could ask the basis team to give me a copy. Any help would be greatly appreciated.
    Regards
    Preetam

    Hi Adam,
    I have created a New EJB Project for my proxy development. This is the usual way I develop proxies for any given service interface created in ESR. Now i am not sure how to create  a dependency with the BRMS facade. If i find the jars i can add it as an external library when compiling.
    Can you explain a bit more about what you ment by "developing the java proxy in a development component", how did you accomplish this when creating a proxy for a service interface?
    http://help.sap.com/saphelp_nw73ehp1/helpdata/en/e2/3620cef55842cca4632664212f31b1/content.htm?frameset=%2Fen%2F81%2F097529e0e545e58bb2d7a34e4d0a2a%2Fframeset.htm

  • JAR files for SQLJ and JDBC drivers: what is the best practice?

    starting a migration from IAS 10 to WebLogic 11g.
    Apparently the jar files for SQLJ are not on the classpath by default.
    java.lang.NoClassDefFoundError: sqlj/runtime/ref/DefaultContextwhich is the better practice: putting the SQLJ runtime jar into the lib subdirectory of the domain directory, or using a shared library reference? (usage of SQLJ is pretty prevalent in our apps, though we may be getting away from it)
    are the Oracle JDBC drivers on the classpath by default?
    if not, then the same question: put them into the lib subdirectory of the domain directory, or use a shared library reference?

    I'm looking at the setDomainEnv, especially the big note at the top:
    >
    # WARNING: This file is created by the Configuration Wizard.
    # Any changes to this script may be lost when adding extensions to this configuration.
    >
    and am getting squeamish about editing it...
    http://www.bea-weblogic.com/how-do-i-disable-wls-automatically-adding-to-classpath.html looks like the default behaviour is for WebLogic to put $DOMAIN/lib;$WL_HOME/common/lib/ext;$WL_HOME/server/lib/ext on the classpath; there is also a reference to setting weblogic.ext.dirs= when starting weblogic (which means I set the WEBLOGIC_EXTENSION_DIRS environment variable).
    http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/libraries.html#wp1067450 also refers at the bottom to using the domain /lib subdirectory.
    so am I correct that a good practice is to just put the jars I think I will globally need into $DOMAIN/lib, rather than putting them in $WL_HOME/common/lib/ext, $WL_HOME/server/lib/ext, or fiddling with the WEBLOGIC_EXTENSION_DIRS environment variable?
    Edited by: user8652010 on Feb 10, 2011 1:08 PM

  • How to use 3rd party jar files for xml transform

    Hi all,
    Am using OAS9.0.4 and I deployed my .ear successfully.
    In my application am using 4 jar files for xml transformation. (xerces.jar, xalan.jar, xml-apis.jar,xercesImpl.jar)
    OAS has its own xmlparserv2.jar file. Its using this jar file for transforming xml data and not even looking for above mentioned 4 jar files.
    In opmn.xml, I provided these 4 jar files in to class path. Then also it didn't effect my output.
    I also tried as put these 4 jar files into j2ee\home\applib directory, but o result.
    Can anybody tell me where should I keep these 4 jar files in OAS and OAS should use these 4 jar files instead of its own xmlparserv2.jar for transforming data.
    Thanks in Anticipation,
    Regards,
    RK

    Add the Xbootclasspath variable in the java-options field. It will somewhat look like the following in your opmn.xml file
    <process-type id="<OC4J Instance Name>" module-id="OC4J">
    <module-data>
    <category id="start-parameters">
    <data id="java-options value="........... -Xbootclasspath^/a:<location of your jar files>" />
    Ensure to use th ^/a (append) signature
    Regards,
    Chirag

  • Jar files for Didgital Signing

    Hi All,
    I have a requirement where I need to Access to Keystore for Digital signing an XML payload. I am using the example in the SAP help link - Link: [Using Digital Signatures|http://help.sap.com/saphelp_nw04s/helpdata/en/a4/d0201854fb6a4cb9545892b49d4851/frameset.htm] 
    I have already imported the following jar files (aii_af_cci.jar,aii_af_cpa.jar,aii_af_mp.jar, aii_af_ms_api.jar, aii_af_ms_spi.jar, aii_af_svc.jar, aii_af_trace.jar, aii_security_lib.jar, aii_security_interface.jar, iaik_jce.jar, tc_sec_ssf.jar).
    The code has the following import statements:
    import com.sap.aii.af.mp.module.*;
    import com.sap.aii.af.ra.ms.api.*;
    import com.sap.aii.af.mp.module.*;
    import com.sap.aii.af.ra.ms.api.*;
    import com.sap.aii.af.service.auditlog.*;
    import com.sap.aii.af.service.resource.SAPSecurityResources;
    import com.sap.aii.security.lib.KeyStoreManager;
    import com.sap.aii.security.lib.PermissionMode;
    import com.sap.security.core.server.ssf.SsfProfileKeyStore;
    import com.sap.security.api.ssf.ISsfProfile;
    import java.io.UnsupportedEncodingException;
    But even after the above imports I am still getting errors in resolving the following variables in the code.
      KeystoreManager
      keyStore
    What are import statements or jar files I am missing? and where can i find those jar files for making the example work?
    Thanks and Regards,
    Arunava
    Edited by: Arunava Das on Jun 6, 2008 7:05 PM

    Hi All,
    Im working on code which has
    import com.sap.security.api.ssf.ISsfProfile
    But i couldn't find the jar file for that. I think the the package is "com.sap.security.api.ssf".
    Please help me out, where can i get that jar file...?
    Warm Regards,
    DNK Siddhardha.

  • Jar file for com.sapportals.wcm.service.propertyconfig

    Hi all,
    Where can i find the jar file for <b>com.sapportals.wcm.service.propertyconfig</b>.
    <b></b>
    If anyone can send that to my mail id(<b>[email protected]</b>) it would be very useful.
    Points will be rewarded for helpful answers.
    Regards
    Rajeev.

    Hi Rajeev,
    the files should be under /usr/sap/<SID>/JC00/j2ee/cluster/server0/apps/sap.com/irj/servlet_jsp/irj/root/web-inf/portal/portalapps/com.sap.km.cm.service.base
    or you can find it in the par file com.sap.km.cm.service.base.par
    Regards,
    Pascal

Maybe you are looking for

  • Installing Linksys Wireless G WAP54G Router

    So...the guy at Staples said it would be fine with a Mac. Yea right. It doesn't recognize the installation disc, the help on the Linksys site may as well be written in another language for someone like me. Is there anyone who can explain to me how to

  • Using Java UFL with BOE XI

    Post Author: Elvyn CA Forum: Migration to XI R2 We are using CR4E deployed on Linux. Some of our reports use Java UFLs.We are currently migrating to BOE XI and can't find support for Java UFLs. 1) Is there a way to use Java UFL with BOE XI (on Linux)

  • Is there a way to edit rules in underline options?

    A client is asking for more space between double rules being used in underline options. Does anybody know if the preset rules in InDesign can be modified or is there a way I can create/customize my own?

  • Technical help needed  for button click

    Hi All, Im new to abap, just  now started working in my first project. I need some help and start from you. In my webdynpro component FPM_OIF_COMPONENT  : CNR_VIEW, Participate button  is there, so by default u201CCreate Responseu201C button is graye

  • Stopping of confirmation & GR in absence of goods issue

    Hi System is allowing me to confirm an operation (CO11N) and do GR(MB31) evenif the input materials are not issued. But the requirement is atleast  one material (not all) should have been issued. How to do that? Thanks & regds