Re: Compile java sources inside J2EE component

From: Chris Uppal (chris.uppal_at_metagnostic.REMOVE-THIS.org)
Date: 09/07/04


Date: Tue, 7 Sep 2004 10:50:29 +0100

Stefan Siegl wrote:

> So much for the introduction ;). After creating the source files I need
> to compile them at runtime, is there a possibility to do so?

Yes. You can invoke Sun's compiler directly (I can't remember the precise
class off-hand, but it's mentioned in another thread in this newsgroup today).
Andrew's warning is to be taken seriously, but I wouldn't let it worry /me/ --
I believe that Sun are intending to provide a public API to compilation soon
(and that it only missed 1.5 because of time pressure), so I'd hope that if Sun
do remove the private classes, then they'll replace them with something else
you can use. You can always fall back to using javac if it really becomes
necessary, anyway.

However, if /I/ were doing this then I think I'd look at the option of
interpreting the meta-model directly, rather than generating new classes. I
think that would be easier, and I can't imagine it causing performance
problems. Still, there's nothing wrong with generating classes, if that's
what appeals to you, so...

> As I said before if the model changes, the new source code files need to
> be compiled again. Is there a way to "unload" possible classes that were
> created for the last model, so that there won't be a clash with the
> newly created classes?

You need to load all the "disposable" classes through a specially created
classloader (the standard URLClassLoader, or whatever it's called, should work
OK, but you might want to write your own anyway). When you drop the reference
to the classloader, and when there are no more instances of the classes
themselves alive, then the classes will be removed from the runtime. When you
want to create a new batch of classes, you create a new classloader to load
them, so there's no interference with any defunct classes that might still be
hanging around.

You'll probably find it worthwhile to define a bunch of interfaces that are
/not/ disposable, and which the various generated classes implement. Otherwise
you'll have difficulty "talking about" instances of the disposable classes in
other contexts.

One thing that isn't helped by classloaders is migrating the state of the
system as it is modified. If you create a new "version" of a class, the
instances of the existing class won't be migrated. You'll have to transfer the
state of the system from the old collection of objects to the new. Somehow...

Again, I think that migration would probably be easier if you were interpreting
the meta-model, and that's one reason why I'd consider that first.

    -- chris



Relevant Pages

  • Re: two public class in a single file
    ... As a result you can have only one public class in a file. ... several .class files such the classloader could find all of them.) ... Having only one public class per source file, the compiler can find the source ... B.java, compile that first, and then compile A.java. ...
    (comp.lang.java.programmer)
  • try/catch unreachable code C2407 migrating from vs 6.0 to vs 7.1 .NET
    ... I am migrating C++ unmanaged to managed VC 7.1. ... The compile tells ... prefix "public _gc class CAccountExposure: ... statements that automate excel ...
    (microsoft.public.dotnet.languages.vc)
  • try/catch unreachable code C2407 migrating from vs 6.0 to vs 7.1 .NET
    ... I am migrating C++ unmanaged to managed VC 7.1. ... The compile tells ... prefix "public _gc class CAccountExposure: ... statements that automate excel ...
    (microsoft.public.vc.language)
  • f2c.. what happened?
    ... I'm migrating to red hat and I noticed that there is no f2c in version 9 ... I need it to compile some programs.. ... -- redhat-list mailing list unsubscribe mailto:redhat-list-request@redhat.com?subject=unsubscribe ...
    (RedHat)
  • std::_MAX Replacement
    ... I'm migrating from VS6.0 to VS 2003 .Net. ... One of the breaking changes is that _MAX has been redefined so that ... but that won't compile either. ... Prev by Date: ...
    (microsoft.public.vc.language)