问题
I'm currently writing on a tool that is client server based. Because of unstable IP-Adresses, my idea is to let the server-Application compile a client-application with the right IP of the server.
Step-by-step:
- The serverapplication starts.
- The user looks, what current IP his server-computer has (this can be done manually over a webbrowser).
- With this IP he creates (compiles) a client which search a connection to this IP (once started).
- To make this possible, it's necessary to compile the client, after the IP has been set.
- The client runs as background application without any GUI or console-window. So adding the IP on the client side isn't what I search for. The biggest newbies should be able to just run the client and their work ist done.
I tought that I could just unpack javac (out of the Java-application), then write the class to a file (as complete string) and then run a console command, which uses the javac.exe to compile a runnable jar file. But of course it isn't that easy. My problem is that I don't want to put the whole JDK-Folder to the server-application, just to compile the client. Does anyone of you know a proper way how I could handle this?
Important is, that I have to fight with computers, which haven't a JDK installed, just the standard JRE which hasn't a compiler on board (of course).
回答1:
That is definitely not a good way of going about this. There are a couple of alternative approaches which would be much simpler:
Pass the IP address or hostname of the server to the client application as a parameter (e.g, via the command line). This requires no changes to the client at all, but will require you to have some way of passing that parameter on.
If you want to get really clever, you could potentially make the filename of the client application contain the IP (e.g, as a hexadecimal value: a client that connected to
127.0.0.1
might be calledclient-7f000001.jar
). You'd have to make sure it didn't get renamed, though.Embed a text file containing the IP address of the server in the client JAR file. Since a JAR is just a ZIP archive, you don't need a compiler to do this.
来源:https://stackoverflow.com/questions/22417752/compile-javacode-out-of-a-running-java-accpilaction-on-a-system-that-hasnt-jd