users@glassfish.java.net

[gf-users] Re: glassfish4, javacompiler

From: Martin Gainty <mgainty_at_hotmail.com>
Date: Tue, 1 Jul 2014 10:05:26 -0400

Date: Tue, 1 Jul 2014 15:32:49 +0400
From: nikita.nn_at_gmail.com
To: users_at_glassfish.java.net
Subject: [gf-users] glassfish4, javacompiler

using glassfish4, jdk1.7,
errors in task.call() method,
>compiler.err.cant.resolve.location.args
>padm.exforms.MemoryFileManager$JavaSourceFromString[mfm:///CM.java]]]
>cannot find symbol
  symbol: method getVal(int,int)
  location: class padm.exforms.CM]
and etc...
in j2se client that code works ok, in glassfish server get errors


example code;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector();
String className = "CM";
MCG>this wont work
Class<?>_class = getClassFromString(compiler, diagnostics, Arrays.asList("-g:none", "-classpath", System.getProperty("java.class.path")), className, getClassCode(className));


  private Class<?> getClassFromString(JavaCompiler compiler, DiagnosticListener<JavaFileObject> listener, List<String> flags, String className, String classCode) {
    MemoryFileManager mfm = new MemoryFileManager(compiler.getStandardFileManager(listener, null, null));

    JavaFileObject fileObject = MemoryFileManager.makeSource(className, classCode);

    CompilationTask task = compiler.getTask(null, mfm, listener, flags, null, Arrays.asList(fileObject));
    if (task.call()) { //error in this call

      ClassLoader cl = mfm.getClassLoader(StandardLocation.CLASS_OUTPUT);
MCG>the classloader used by org.vertx.java.platform.impl.java.MemoryFileManager
MCG> is the only arbiter of how consequent classes will be loaded by that classloader an example:
Enumeration<URL> urlEnumeration = classLoader.getResources(javaPackageName);
MCG>I have no idea where you are running this code so and which classloader you are using
MCG>this will give you some clue as to the plethora of classloaders used by Glassfish
MCG>http://java.sun.com/developer/technicalArticles/Networking/classloaders/
MCG>your best bet is to find the jar which contains MemoryFileManager e.g.
MCG>grep -r -l org.vertx.java.platform.impl.java
MCG>you should find the jar which contains MemoryFileManager (should be vertx-platform*.jar)
MCG>mkdir jar
MCG>cd jar
MCG>cp vertx-platform*.jar jar
MCG>jar -xvf vertx-platform
MCG>protect the original manifest
MCG>chmod 111 ./META-INF/MANIFEST.MF
MCG>using grep find jar as shown above to locate jar which contains your new classes
MCG>cp found_jar.jar jar
MCG>jar -xvf found_jar.jar
MCG>jar up the whole mess
MCG>jar -cvf vertx-platform.jar
MCG>cp vertx-platform.jar ON TOP OF AND OVERWRITE IN PLACE the old vertx-platform*.jar
https://glassfish.java.net/docs/4.0/application-development-guide.pdf

Martin
--------------------
(shameless plugin for ESPN)..watch Brazilian World Cup 2014 Live Coverage at
http://espn.go.com/watchespn/sitemap/_/sport/soccer-futbol/league/2014-fifa-world-cup