0

I got a little project where I have to compute a list. The computation depends on serveal factors. The point is that these factors change from time to time and the user should be allowed to change this by it's self.

Up to now, the factors are hard-coded and no changes can be done without recompiling the code.

At the moment the code looks like this:

    if (someStatement.equals("someString")) {
        computedList.remove("something");
        }

My idea is to make an editable and human readable textfile, configfile, etc. which is loaded at runtime/ at startup? This file should hold the java code from above.

Any ideas how to do that? Please note: The targeted PCs do not have the JDK installed, only an JRE.

gagamel1989
  • 9
  • 1
  • 4
  • 1
    A **human readable** file containing a java code snipplet? That's contradictory since not every human knows java nor does everyone know what these fields/variables represent. – fabian Mar 17 '15 at 14:27
  • You can persume that the users are able to read the simple code fragments ;) – gagamel1989 Mar 17 '15 at 14:31
  • Wait a sec... Your comments to sallamender's answer implies that you don't want an java interpreter but just want to remove certain strings from a list if a string equals some other String. That would make things much simpler... – fabian Mar 17 '15 at 14:46
  • Right - but the computed list is processed in other parts of the program. So this list is just some kind of "input" for the programm... – gagamel1989 Mar 19 '15 at 06:03

4 Answers4

0

An effective way of going about this is using a static initializer. Static Block in Java A good and concise explanation can be found under this link.

Community
  • 1
  • 1
Decagi
  • 21
  • 2
0

One option here that would allow this would be to use User Input Dialogs from the swing API - then you could store the users answer's in variables and export them to a text file/config file, or just use them right in the program without saving them. You would just have the input dialogs pop up at the very beginning of the program before anything else happens, and then the program would run based off those responses.

  • I was thinking about something like that - but I got about 150 of these IF-cases... So that would be quite time consuming – gagamel1989 Mar 17 '15 at 14:21
  • Okay, yes that would be brutal! Is there any way to set some of them to default cases? Another option would be to create several frames with multiple choices, where you set the defaults and simply let the user choose if they want to change them. Even, that, though, could be a lot (i.e. 10 frames for 15 choices each would still be a ton). Let me know what you up deciding on, please, if you think about it. – sallamander Mar 17 '15 at 14:29
0

You could use Javascript for the configuration file language, instead of java. Java 7 SE and later includes a javascript interpreter that you can call from Java. it's not difficult to use, and you can inject java objects into the javascript environment.

Basically, you'd create a Javascript environment, insert the java objects into it which the config file is expected to configure, and then run the config file as javascript.

Kenster
  • 23,465
  • 21
  • 80
  • 106
0

Okay, here we go... I found an quite simple solution for my problem. I am using Janino by Codehaus (Link). This library has an integrated Java compiler and seems to work like the JavaCompiler class in Java 7. BUT without having the JDK to be installed.

Through Janino you can load and compile *.java files(which are human readable) at runtime, which was exactly what I needed.

I think the examples and code-snippets on their homepage are just painful, so here's my own implementation:

Step one is to implement an interface with the same methods your Java file has which is loaded at runtime:

public interface ZuordnungInterface {

public ArrayList<String> Zuordnung(ArrayList<String> rawList);}

Then you call the Janino classloader when you need the class:

File janinoSourceDir = new File(PATH_TO_JAVAFILE);
    File[] srcDir = new File[] { janinoSourceDir };
    String encoding = null;
    ClassLoader parentClassLoader = this.getClass().getClassLoader();
    ClassLoader cl = new JavaSourceClassLoader(parentClassLoader, srcDir,
            encoding);

And create an new instance

ZuordnungsInterface myZuordnung = (ZuordnungInterface) cl.loadClass("zuordnung")
                .newInstance();

Note: The class which is loaded is named zuordnung.java, so there is no extension needed in the call cl.loadClass("zuordnung").

And finaly the class I want to load and compile at runtime of my program, which can be located wherever you want it to be (PATH_TO_JAVAFILE):

public class zuordnung implements ZuordnungInterface {
public ArrayList<String> Zuordnung(ArrayList<String> rawList){

    ArrayList<String> computedList = (ArrayList<String>) rawList.clone();

    if (Model.getSomeString().equals("Some other string")) {
        computedList.add("Yeah, I loaded an external Java class");
    }


    return computedList;
}}

That's it. Hope it helps others with similar problems!

gagamel1989
  • 9
  • 1
  • 4