How to load a .java file into a variable? [duplicate] - java

This question already has answers here:
What is a Java ClassLoader?
(8 answers)
Closed 5 months ago.
I'm still fairly new to java and currently working on a text-based adventure game as a practice project.
The engine loads scenes to play that are all child-classes from a "Scene" superclass and appear as for eg. "dungeon.java". Now I want the game to be expandable.
The ideas is that a user can drop new scenes as .java-files into a "Scenes" folder and everytime the game is launched it reads all files in that folder and safes them into a "Scene"-class array.
My problem is that I don't know how to code this. I googled a lot and tried various phrasings but all I can find are tutorials for reading lines from txt-files or similar.
Is it even possible to read a complete file into a variable without serialzation?
I already tried the following code, but didn't get around to test it yet.
private static void buildScenePool() {
File scenesFolder = new File("/scenes");
\\ setup filter for .java files
FilenameFilter javafilter = (dir, name) -> name.endsWith(".java");
File[] sceneList = scenesFolder.listFiles(javafilter);
\\ create new arry large enough for all scenes
allScenes = new Scene[sceneList.length];
try{
FileInputStream fileIn = new FileInputStream(scenesFolder);
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
\\ iterate trough the list array and safe files to array
for (int x = 0; x < allScenes.length; x++) {
allScenes[x] = objectIn.readObject( (Scene)sceneList[x] );
}
objectIn.close;
} catch (IOException e) {
System.err.println(e.toString());
}
}

This is rather very complicated. java code is normally compiled, and thus, that means you'd have to scan for a new java file, compile it (which is its own complicated ordeal, as that also means setting up the classpath and the like properly so that the compiler knows what to do), then load in the class file the compiler produced, and then call the appropriate methods in there.
You can do that. It's just, quite complicated. Your standard JVM doesn't necessarily even ship with a compiler; this is solvable too (either demand that this runs only on one that does, and the modern deployment rules for java involve you getting a JVM on your user's machines, so you thus pick one that does include a compiler – or you can just ship the compiler as dependency with your app, javac is itself a java app and runs on any JVM).
However, the more usual approach is to not actually use java for this. Instead, use something java-like, but not java: Scripting languages, like groovy, or javascript (okay, that is not particularly java-like perhaps).
That is its own sort of complication. There are no easy answers to any of this.
I think it's time to first think broad strokes and determine how you want the user's experience (that is, a user that wants to add a scene) should be, and then ask a new SO question about your specific choice.
You write em, they install em
In this model, users simply download or pick a 'scene' impl that someone else wrote (a 'real' programmer with a full fork of the entire source code, an IDE, a build tool, the works). Don't knock it - programming is hard, and saying: "Oh, look, anybody can customize a scene, it's easy, just open notepad.exe, write something like this (example java file here), and dump it in the Scene folder and off you go!", but this is not at all easy. To us programmers that seems familiar at least, but to your average user you're asking them to just rattle off a poem in ancient sumerian - normal people don't program. And if they do program, they're programmers. They would much rather get instructions about how to fork a project and set it up in an IDE than some bizarreness about tossing raw java files someplace.
Scripting
This is what Processing (for programming arduinos) does, more or less what webbrowsers did (which explains why javascript is so weird), and most 'plugin systems' for pseudo-smart editors like TextMate and Emacs do: You script them. As in, you fully buy into the idea that the custom stuff written by the user is extremely simple and highly targeted (really only makes sense to run within the confines of your app, not as standalone stuff - and dependencies don't generally come up), and pick a language that fits that model, and java certainly is not that.
Obvious options are javascript and groovy. This is certainly possible but not at all easy. Still, if you're interested, search the web for tutorials on how to run javascript or groovy inside a JVM, and you'll get plenty of hits.
Java code, and you compile it
That's what your question is positing as only option. I don't recommend it, but you can do this if you must. Be aware that it seems to me, based on the way you worded your question and your example code which makes various newbie mistakes (such as print-and-continue exception handling, which is always wrong, using obsolete APIs, and messing with built-in serialization) that this is a few ballparks beyond your current skillset. A challenge is always cool, so, if you want to go for it, you should! Just be aware it'll be the most difficult thing you've ever written and it'll take a few fully dedicated weeks, with a lot of reading and experimenting.
Just definitions, really
The central tenet so far has been that you can actually program. Instructions that make the machine act in certain ways. Possibly you don't need any of that. If a Scene is really just a background colour and a few widgets displayed here and there, should it even be code at all? Maybe you just want a declarative setup: The plugin/scene writer just declares certain properties and that's all they get to do, you just 'run' such a declarative definition. In which case the 'language' of the declaration can be in JSON, XML, YAML, TOML, or any other format designed for configuration files and such, and you can forego the hairy business of attempting to compile/run user-provided code in the first place.

In order to load the Java classes into your application, you need to compile them. You could do this from Java by invoking the javac executable. See Starting a process in Java? for instructions on how you could do that. Once compiled, you'd then need to load the classes into the JVM using a class loader, e.g. by invoking ClassLoader.defineClass. You probably want to configure a protection domain as well, to prevent user provided classes from misbehaving.
However, Java might not be the best approach for extending your application. You could consider using a scripting language instead, like JavaScript. See Nashorn (an open source script engine that was included in previous versions of Java, and that can now be downloaded separately) and the Java Scripting Programmer's Guide for more information.

Related

Wanting to convert CFScript source code into Java source code

I am in the process of moving some old ColdFusion source code across to Java. Most of it is in CFScript format and this does a good enough job of converting CFML to CFScript: https://cfscript.me/ (Thanks Pete Freitag)
I wonder if there are utilities to convert CFScript source to Java source code. Note: I am NOT talking about decompiling java class bytecode.
The CFScript syntax is pretty close to Java syntax and I am looking for a way to do some of the heavy lifting to reduce the amount of hand-editing.
Any thoughts? If not, I might have a go at writing something.
Thanks,
Murray
EDIT: More background:
I want to migrate an application completely from CF to Java. But, it is a big app and my idea is to incrementally create "drop in" java classes to replace existing CFCs and write new code as Java classes in the meantime. I can execute a Java class in CF and use CFCs in Java via createDynamicProxy().
At its most simple form I can then change from
someObj = createObject("component","SomeCFC");
msg = someObj.getMessage();
to
someObj = createObject("java","SomeClass");
msg = someObj.getMessage();
I am not looking to "port" it (as we would say in the old days ;-) ), just to run the CFC through a "parser" that did some of the obvious code changes before I then work through and fix the harder stuff and refactor. That way, at least I have the business logic and same methods and I can run it through the tests.
This might be pie-in-the-sky but I imagine I am not the first person who needed to go down this particular migration path.

How to Programmatically Modify Java Source Code

I am going to need the ability to programmatically modify Java source code, specifically apply refactorings. For example moving a method from one class to another, changing an access modifier from public to private, etc.
Now in C# I'd probably go the abstract syntax tree / Roslyn approach, but I have no idea where to even begin in Java.
Given a java source code file, how does one parse it so that modifications can be made and then saved to it?
First: Things like that (called refactoring) are already handled by a good Java IDE (Eclipse, IntelliJ IDEA, NetBeans and you name it). I really think that stuff like that should be handled by an IDE.
Second: If you really want to edit the files programmatically keep in mind that you're the one who is in charge of format and syntax. You have to read the files, edit them (cut out, paste in or add some code at proper line) and save them. The link provided from VedX is a good start for basic IO. But there is so much more to keep track of.
I did something similar with OpenEdge ABL and it is no easy task (you'll find things that went wrong you never thought of and if you didn't backed up your code it's f**ked up). I've got it done but in the end it wasn't a good trade-off between time spend to get it done and actual usefullness (I just needed it about 4 times for approx. 1500 files).
Finally (after I lost many hours inventing the wheel of refactoring) I wrote some class generators which wrote a whole new file evertime I needed to change something - lastly I only changed the class generators (maybe this is something you didn't thought of and it fits too). Now it's automatized and generic and could run in parallel (something what OpenEdge isn't designed for - okay it's a cheap hack by me).
Third: Maybe there is a package you can use in your Java project. I would try to Google it. My first attempt resulted in this: http://www.methodsandtools.com/archive/archive.php?id=4 for first understanding what it's all about and what to consider.

When do new things get added to the Java API

I have been wondering about this for a while and I can’t really find a clear answer. You see the standard Java API is really big and it includes a lot of different libraries and classes for you to use from GUI design to sending data over the Internet to basic things like sending a String to the console.
It also includes things like reading MIDI generating secure random Strings, things that seem really specific. But at the same time there doesn’t seem to be any standard JSON libraries available while JSON is an universal way of sending data between systems.
So what I want to know is: When does something get added to the Java API? What does something need to be considered to be added to the API?
There is a "framework" that drives how new features "get" into java; to manifest themselves later on as new language elements or libraries.
Enter ... the Java Community Process!
Meaning: this is a forum where people make suggestions; which then get discussed; and at some point are either "added to Java somehow"; or rejected.
And for starters: the JSON-P project about a JSON processing API was/is driven by the jcp, see entry 374.
Finally: but you are correct, not everything that shows up in the "standard library" should be there; whereas other important parts take way too long before people can agree on a proposal. And of course, there is also a long history of evolution.
So: when you could restart Java from scratch; you would organize things in a different way (and to a certain degree, that is what Java9 is trying to enable with the new module concept).

Compile a Java program on the server right before a User downloads the program

I did a quick search and could not find anything on this topic. I am not even sure if this is possible, but I am curious.
Is it possible to compile a Java program on a server right before a user downloads the program. The application that I can think of for this would be to modify a program's source code on the fly before a user downloads it. This could be helpful in a setup where the program is modified based on user input or settings on a website and those changes are hard baked into the program so what they download is a stand alone program that is customized and fully portable. The other application I thought of would be if each user were to use a different feature combination in a program so it is compiled only with the feature set they need/want on the fly.
I have a few programs ideas that I could test this out with, but this is mostly an academic thought and curiosity of mine.
So long story short, does anyone know of any technologies that could make a system like this work?
Sure, it's possible.
Just let the download link point to some script, that compiles / packages the source and sends back the result. This can be implemented in, for instance PHP, in just a few lines of code. It's quite similar to captchas: On-the-fly generated unique data, retrieved through a URL.
I myself have thought about this idea for protocol obfuscation purposes and for "software registration key algorithm" generation.
I would however recommend you to factor out the parts which you want to be recompiled into a separate class / set of classes, compile only these, and package it with the rest of the (already compiled) program upon request.
I have written a library using the Compile API (comes with Java 6) to compile code in ememory but I would suggest you don't need to pre-generate code as anything you can do with generated code you can do with dynamic code. There can be a slight performance advantage, but I suggest you try doing what you need with dynamic code (i.e. code with loops, if statements and reflection) to do what your generated code would do first as this is alot simpler and likely to do what you want.
Even if you must have generated code, it is useful to write the code in a non-generated form first so you are clear as to what you need the code to do.

Using noweb on a large Java project

Has anyone used the noweb literate programming tool on a large Java project, where several source code files must be generated in different subdirectories? How did you manage this with noweb? Are there any resources and/or best practices out there?
Noweb will dump out files relative to the current working directory, or at the absolute path you specify. Just don't use * at the end of your filename (to avoid inserting the # preprocessor directives). I would recommend using %def with # to show where you define and use names.
<</path/to/file.java>>=
reallyImportantVariable += 1;
# %def reallyImportantVariable
noweb lets you reorder and (the real win) reuse snippets of code, which I don't think javac would understand.
I'd agree that since most people expect that you'll use javadoc, you're probably swimming against the stream to use noweb.
Literate Programming works its best if the generated intermediate code can point back to the original source file to allow debugging, and analyzing compiler errors. This usually means pre processor support, which Java doesn't support.
Additionally Literate Programming is really not necessary for Java, as the original need for a strict sequential order - which was what prompted Knuth to write a tool to put snippets together in the appropriate sequence - is not present. The final benefit of literate programming, namely being able to write prose about the code, is also available as Javadoc which allow you to put everything in as comments.
To me, there is no benefit in literate programming for Java, and only troubles (just imagine getting IDE support).
Any particular reason you are considering it?

Categories