1

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());
    }   
}

2 Answers2

2

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.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks for your extensive answer! I did some more research and found my error. I wrongly assumed that .java files are already the compiled classes. That's why my question must seem so confusing and complicated. I never planned to have user-ceated content or release uncompiled files. What I actually want to do is have a base game with some included scenes, but be able to basically provide DLCs so to speak. I can export new set of scenes as JAR file, which contains the .class files, that then can be dropped into a Scenes folder and read with `ClassLoader`, right? – SquishySquid Sep 28 '22 at 12:27
  • Sure, that would work. – rzwitserloot Sep 28 '22 at 12:44
0

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.

markusk
  • 6,477
  • 34
  • 39