1

I have a question which I'm pretty confused from.

I am aware of the differences between Java Runtime Enviroment and Java Developement Kit.

I'm writing a program that uses the ToolProvider.getSystemJavaCompiler() method to compile java code from within the code.

Now, I've been answered that I can't compile code from client side if my client doesn't have JDK installed. My main question is, how can I do that? I don't want my clients having to install JDK on their computer just to run my program.

Thanks in advance!

NonameSL
  • 1,405
  • 14
  • 27

4 Answers4

0

You need to compile it on your system, and distribute the class file of corresponding java source file to anyone.

That class file doesn't require JDK but JRE must be installed on that system to run the class file.

Manish Singh
  • 518
  • 3
  • 13
  • 1
    Not for `getSystemJavaCompiler`. Read the question. – SLaks Oct 23 '16 at 17:05
  • Yea earlier it was different. :) Though, In simple terms you cannot compile the code until and unless you have javac i.e. which comes with JDK. Hope it solves your query. – Manish Singh Oct 23 '16 at 17:12
0

If you want to compile code, you need a compiler, so if the user can't be expected to have the compiler you need, you'll simply have to bundle it.

I really can't say I know how to bundle the standard javac compiler, though it's probably possible, strictly speaking, to find the Jar file that contains it and bundle that along with your code. No idea how robust such a solution would be, though.

But depending on your needs, you may not need the standard javac. There are tons of byte-code generation libraries out there, with more or less high-level functionality. I wouldn't really want to recommend anything that I have no personal experience with, but examples include Byte Buddy or ASM. You could probably use ABCL too.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
0

Eclipse's compiler is worth a look as well.

There is also an so question here.

Community
  • 1
  • 1
Ravindra HV
  • 2,558
  • 1
  • 17
  • 26
0

So there really is no way to do what it is you are wanting to do unless you bundle the compiler itself with you application, or unless you find a library that has all of the Java compiler code in it already so it doesn't have to use the JDK compiler, you will not get what you want, and what you want is the ability to turn a String containing source code into a Java class.

I do not understand what you wish to accomplish, but the BEST option I can give you is asm. If you are up for the task, you can manually write new classes at runtime without the presence of the JDK compiler. HOWEVER, this does not involve you using a String full of source code and turning it into a Class object. This is you working at the low level with the Java bytecode for the most part.

This tutorial can get you started: https://www.javaworld.com/article/2071777/design-patterns/add-dynamic-java-code-to-your-application.html

And here is the Java documentation for class files. You can use this to expand on what you learned from the first link: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

That is the only instance creating classes on the fly that I can give you. That being said, you could try writing your own Java compiler that can turn source code into classes without ever getting the Java compiler, but at that point you are literally recreating the Java compiler yourself, and I assure you that is no easy feat for one person.

Clayton
  • 96
  • 3