質問

I am progamming a game in which the player navigates puzzles by changing the source code of objects within the levels themselves. In order to do this I have a class extending JPanel which is to be written over with the edited source code from the player. My question is how can I compile a class file from a text file, either using java utilities or using beanshell? The text file contains pure java but the player of the game may cause compilation or runtime errors (such as infinite loops) and I want to be able to catch said errors and alert the player.

役に立ちましたか?

解決

Sounds like a great idea, but dynamically compiling classes and getting them to execute requires quite a lot of work. There is a library called Janino that (in their own words):

Janino is a super-small, super-fast Java™ compiler. Not only can it compile a set of source files to a set of class files like JAVAC, but also can it compile a Java™ expression, block, class body or source file in memory, load the bytecode and execute it directly in the same JVM.

By delegating the work related to actually compiling and running the dynamic code to Janino you can focus on the actual game logic.

他のヒント

If you dont want to use an external library, you need to write your own ClassLoader. The most simple implementation i found is here.

/*
 * Copyright 2008-2010 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package com.sun.btrace;

/**
 * A simple class loader that loads from byte buffer.
 *
 * @author A. Sundararajan
 */
final class MemoryClassLoader extends ClassLoader {
    MemoryClassLoader() {
        this(null);
    }

    MemoryClassLoader(ClassLoader parent) {
        super(parent);
    }

    Class loadClass(String className, byte[] buf) 
        throws ClassNotFoundException {
        return defineClass(className, buf, 0, buf.length);
    }
}

You could consider to use a scripting language supported by the JVM such as Groovy for the dynamic part.

You can easily do it by changing the "fileName.txt" file extension to "fileName.java" by clicking on name of the file assuming the file contains pure java source code as you said.

Then;

-Open terminal (assuming java jdk is already installed on your computer if not install jdk from Oracle Website) -Come to directory where your file is located. (using cd "directoryLocation") -write "javac fileName.java" -fileName.class file will be created on the same directory -on terminal write "java filename" -then your java file will be executed on the terminal console.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top