Web应用程序中的动态Java编译

Web应用程序中的动态Java编译,第1张

概述我有一个不寻常的问题(嗯,我认为这是不寻常的).我需要能够通过Web界面上传未编译的 Java源文件,并让接收控制器编译它并在该编译的类上执行方法. 我已经有了一些方法 – 我可以上传文件,并成功编译.但是,无论我尝试什么,我都无法加载(新)类定义并实例化它.我不断抛出java.lang.ClassNotFoundException异常. 这是我的代码; String fileName = pat 我有一个不寻常的问题(嗯,我认为这是不寻常的).我需要能够通过Web界面上传未编译的 Java源文件,并让接收控制器编译它并在该编译的类上执行方法.

我已经有了一些方法 – 我可以上传文件,并成功编译.但是,无论我尝试什么,我都无法加载(新)类定义并实例化它.我不断抛出java.lang.classNotFoundException异常.

这是我的代码;

String filename = patchfile.getfileItem().getname();    String fullname = "<package name>" + filename.substring(0,filename.indexOf("."));    // instantiate the Java compiler    JavaCompiler compiler = ToolProvIDer.getSystemJavaCompiler();    JavafileManager fileManager = new ClassfileManager(compiler.getStandardfileManager(null,null,null));    // load the uploaded files into the compiler    List<JavafileObject> files = new ArrayList<JavafileObject>();    files.add(new ByteArrayJavafileObject(fullname,patchfile.getBytes()));    // set the classpath    List<String> options = new ArrayList<String>();    options.add("-classpath");    StringBuilder sb = new StringBuilder();    urlclassloader urlclassloader = (urlclassloader) Thread.currentThread().getContextClassLoader();    for (URL url : urlclassloader.getURLs()) {        sb.append(url.getfile()).append(file.pathSeparator);                }    options.add(sb.toString());    // execute the compiler    compiler.getTask(null,fileManager,options,files).call();    // instantiate the class (FAILS HERE)    Object instance = fileManager.getClassLoader(null).loadClass(fullname).newInstance();    // close the file manager    fileManager.close();

我也尝试过;

urlclassloader.loadClass(fullname).newInstance();

但没有运气……

谁能对此有所了解?

提前致谢,

根据要求,这是堆栈跟踪;

org.springframework.web.util.nestedservletexception: Request processing Failed; nested exception is java.lang.classNotFoundException: com.sifourteen.papyrus.fabricators.patches.TestFormFabricatororg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)javax.servlet.http.httpServlet.service(httpServlet.java:637)javax.servlet.http.httpServlet.service(httpServlet.java:717)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.servletAPI.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:105)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109)org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)org.springframework.orm.hibernate3.support.OpenSessionInVIEwFilter.doFilterInternal(OpenSessionInVIEwFilter.java:198)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)root causejava.lang.classNotFoundException: com.sifourteen.papyrus.fabricators.patches.TestFormFabricator    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)    java.lang.class.forname0(Native Method)    java.lang.class.forname(Class.java:247)    com.sifourteen.papyrus.controllers.adminController.patch(adminController.java:152)    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)    java.lang.reflect.Method.invoke(Method.java:597)    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)    org.springframework.web.servlet.dispatcherServlet.dodispatch(dispatcherServlet.java:790)    org.springframework.web.servlet.dispatcherServlet.doService(dispatcherServlet.java:719)    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)    javax.servlet.http.httpServlet.service(httpServlet.java:637)    javax.servlet.http.httpServlet.service(httpServlet.java:717)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:343)    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:119)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.servletAPI.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:105)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:109)    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:355)    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:149)    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)    org.springframework.orm.hibernate3.support.OpenSessionInVIEwFilter.doFilterInternal(OpenSessionInVIEwFilter.java:198)    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
解决方法 类文件需要在类路径中. (例如-d WEB-INF / classes) 总结

以上是内存溢出为你收集整理的Web应用程序中的动态Java编译全部内容,希望文章能够帮你解决Web应用程序中的动态Java编译所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1053842.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存