System.getProperty(“java.class.path”) does not show “WEB-INF/lib” and the including jars

后端 未结 4 1230
迷失自我
迷失自我 2021-01-07 06:45
String CompilePath = \"abc.java\";
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String classpath = System.getProperty(\"java.class.path\");
System.s         


        
4条回答
  •  悲&欢浪女
    2021-01-07 07:42

    They way you are setting the java.class.path system property is asking for trouble - better not do that. More elegant approach would be to use the -classpath option to pass in the custom classpath. See How do I use JDK6 ToolProvider and JavaCompiler with the context classloader? for details.

    Also this question can be useful reference: Using javax.tools.ToolProvider from a custom classloader?


    As to building the actual classpath, you could cast the context classloader to URLClassLoader and get files from those URLs (as done in this answer).

    Or you could use ServletContext.getRealPath(String) and build the entire classpath by hand:

    ServletConfig cfg = ...; //obtained in Servlet.init(ServletConfig) method
    ServletContex ctx = cfg.getServletContext();
    String realWebInfPath = ctx.getRealPath("WEB-INF/lib");
    //TODO use the realWebInfPath to create a File object and iterate over all JAR files
    

    Warning: both approaches ONLY work if web application is expanded (not WAR file). If it is not expanded, you are out of luck.

提交回复
热议问题