1

Can I make a Java program to generate another java application at runtime.

I want to make a "installer" program, which takes user input and generates an application as per user requirement, instead of just configuring the pre-built application according to the user needs.

I came across this solution - how to compile & run java program in another java program?, but I don't want to make clients install JDK on there computer.

Dynamically create table and Java classes at runtime - which also need JDK, but I got a work around: ToolProvider.getSystemJavaCompiler() returns null - usable with only JRE installed?

  1. Can I make a complete application using above methods?
  2. Is it a bad idea to generate such program?
  3. Can I make Spring and Hibernate applications like that?
  4. Or is there any existing framework for doing so?

(if possible it should create tables in db and generate html files as well. I came across http://velocity.apache.org/, so is it possible to generate java code using that.)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Vaisakh K M
  • 105
  • 1
  • 7
  • Sorry, I am using java 8, and not even aware about it. Thanks for the information. I will look about it. – Vaisakh K M Jun 05 '21 at 09:36
  • 2
    @Abra that is true only for Oracle. There are JREs from other vendors that work just fine. – eis Jun 05 '21 at 09:44
  • @Abra that is not true, please don't spread incorrect information. See for example OpenJDK 11 JREs as packaged by Red Hat: https://developers.redhat.com/products/openjdk/download – eis Jun 06 '21 at 10:59
  • @Abra here's also a link to download Adoptopenjdk OpenJDK JREs: https://adoptopenjdk.net/releases.html and here you can download Amazon Corretto OpenJDK JREs: https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html – eis Jun 06 '21 at 11:10

2 Answers2

2

Your goal doesn't make a lot of sense from a practical perspective. I hope that my answer will help you to understand why.


Can I make a java program to generate another java application at runtime.

Yes you can. But it is a lot of work, especially if the application if complicated.


I want to make a "installer" program, which takes user input and generate an application as per user requirement, instead of just configuring the pre-build application according to the user needs.

That is possible ... in theory.

The problem is that you have to write a program that is capable of reading and understanding the user's requirements, and can then converting those requirements into code. Normally ... this is what a programmer does. Writing a program to do what a programmer does is not practical. (My guess is that it is 20 or more years beyond the "state of the art" of artificial intelligence to do such a thing.)

Now if the problem domain was sufficiently restricted, and the requirements were tightly specified in an unambiguous notation, then it might be feasible to do this. However, benefits of generating a program rather than configuring an existing one (based on the same requirement notation) are pretty small. And probably not worth the effort.


... but I don't want to make clients install JDK on their computer.

If you are generating Java programs you need a Java compiler. So if you insist on using a JRE (in Java 8), you need to include a 3rd party Java compiler in your application.

However, for Java 9 onward this is moot:

  • Oracle no longer provides JRE distributions for Java 9+ so you would need to get your client to use a 3rd-party source for their JRE.

  • You could (should) be using the Java 9+ jlink utility to produce a custom JRE for you application, and that can include the standard Java compiler.

If you are trying to generate code at the bytecode level, your problem is immediately ten times harder.


Sorry, I am using Java 8

Are you aware that Java 8 is "end of life" for commercial use? That is likely to affect your clients.


  1. Can I make a complete application using above methods?

Maybe yes, maybe no. It depends on the problem domain. The more complicated it is, and the more diverse / general the requirements, the harder it will be.

  1. Is it a bad idea to generate such program?

Yes. It is a bad idea. It is a lot more work than writing an application that is configured in the conventional way. (Noting that the configuration could include writing plugins in Java, rules in some scripting language, and so on.)

I would advise only generating source code or bytecodes if you already have a conventional application with most / all of the required functionality that you can use as a prototype for the generated generated code. (If you can't write such a prototype by hand, then writing a generator that will create one is not realistic.)

And even when it is feasible, I would question the wisdom of building a generator. There doesn't seem to be a significant pay-off for the extra effort. (For example, where is the benefit for the end user?)

  1. Can I make spring and hibernate application like that?

I don't see why you couldn't generate such an application. But see 1) and 2).

  1. Or is there any existing frameworks for doing so?

There are frameworks that could be used in some cases:

  • Templating frameworks like Velocity1 can be used to generate Java source code.

  • Bytecode engineering frameworks could be used to generate code directly.


1 - Indeed, I have used Velocity for Java source code generation. It worked, though I'm not convinced it was an ideal solution.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • "I don't want to make clients install JDK on their computer." -> "If you are generating Java programs you have no choice." I don't think this is correct. You could use something like [graalvm](https://www.graalvm.org/) to generate native binaries. – eis Jun 05 '21 at 09:29
  • 1
    Well ... sure ... you can include a 3rd party Java compiler in your application too. Or write your own. But this is a quibble, and it is mooted in the next paragraph anyway. – Stephen C Jun 05 '21 at 09:31
  • How is it mooted by the next paragraph? GraalVM fully supports Java11, and doesn't need JDK to be installed by the client. – eis Jun 05 '21 at 09:36
  • It is mooted because you can't get an Oracle JRE for Java 9 onwards. Meaning that the client needs to install a JDK anyway ... which has a Java compiler in it. (Unless the OP is going to stick with Java 8 for ever. Noting that the license has changed so that the OP's clients can't use the latest Java 8 for commercial purposes anyway. They need to pay for a premium license ...) – Stephen C Jun 05 '21 at 09:38
  • 1
    I don't see how that is relevant. You could use some other vendors JRE, such as Red Hat OpenJDK 11 JRE, or you can just build native executables which don't need a JRE at all. – eis Jun 05 '21 at 09:42
  • You don't need a Java compiler on client side for generating for modify Java classes. If you know what you are doing you can directly build or manipulate the Java byte code e.e. using Java ASM library. – Robert Jun 05 '21 at 10:01
  • I thought about that already. I have to make a project for a training propose. But all project ideas are ".... management systems" and most data structures(entities) are similar (like in HMS admin is manager, and in LMS it is principal). so I though, why I can't make a single software, which can mutate according to need (with a config file of course). These can be later provided though a central server according to need. After many thought experiments searching on internet, I finally thought about this... even though I know it was some what foolishness. But I thought it worth a try, – Vaisakh K M Jun 05 '21 at 10:01
  • Check https://github.com/square/javapoet as well for Java code generation – Alberto S. Jun 05 '21 at 10:29
1

Sure you can. You can also leverage a project like GraalVM to generate native binaries for a given platform.

However, it is a lot of work, and the end result won't probably be as useful as you think. Any use case you have in mind will probably be a lot better served by an app that you just configure to do different tasks, so your efforts are probably best spent in that direction.

eis
  • 51,991
  • 13
  • 150
  • 199