0

I'm currently trying to compile a class at runtime but for some reason it's only working on one system. Both systems use the exact same code and have the same java version installed, but on one system my .java file compiles to a .class and on the other system I'm getting exceptions because some classes which are, or should be, in the classpath can't be found.

The code I use to compile it is this:

private static File compile(File file) {
    try {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        List<String> optionList = new ArrayList<>();
        File jar = getJar(RuntimeCompiler.class);
        File pluginDirectory = new File(jar.getAbsolutePath().substring(0, jar.getAbsolutePath().length() - jar.getName().length()));
        String classes = buildClassPath(getJar(Bukkit.class).getAbsolutePath(), pluginDirectory.getAbsolutePath() + "/*");
        optionList.addAll(Arrays.asList("-classpath",classes));
        boolean success;
        try (StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null )) {
            Iterable<? extends JavaFileObject> units;
            units = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file));
            JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, optionList, null, units);
            success = task.call();
        }
        if(success) {
            return new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 5) + ".class");
        }
        else {
            return null;
        }
    } catch (IOException ex) {
        Logger.getLogger(RuntimeCompiler.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}
private static String buildClassPath(String... paths) {
    StringBuilder sb = new StringBuilder();
    for (String path : paths) {
        if (path.endsWith("*")) {
            path = path.substring(0, path.length() - 1);
            File pathFile = new File(path);
            for (File file : pathFile.listFiles()) {
                if (file.isFile() && file.getName().endsWith(".jar")) {
                    sb.append(path);
                    sb.append(file.getName());
                    sb.append(System.getProperty("path.separator"));
                }
            }
        } else {
            sb.append(path);
            sb.append(System.getProperty("path.separator"));
        }
    }
    String s = sb.toString();
    s = s.substring(0,s.length() - 1);
    return s;
}

classpath (optionList.toString()) with Core-1.0-SNAPSHOT.jar containing the necessary files.

[-classpath, /usr/local/gpx/users/user/127.0.0.1:25702/spigot.jar:25702/plugins/Core-1.0-SNAPSHOT.jar]

Stacktrace

/usr/local/gpx/users/user/127.0.0.1:25702/plugins/debug/DebugClass.java:1: error: package     net.nowcraft.core does not exist
[14:04:10] [Server thread/WARN]: import net.nowcraft.core.core;
[14:04:10] [Server thread/WARN]:                         ^
[14:04:10] [Server thread/WARN]:     /usr/local/gpx/users/user/127.0.0.1:25702/plugins/debug/DebugClass.java:2: error: package net.nowcraft.core.RuntimeCompiler does not exist
[14:04:10] [Server thread/WARN]: import net.nowcraft.core.RuntimeCompiler.Debugger
[14:04:10] [Server thread/WARN]:                                         
[14:04:10] [Server thread/WARN]: /usr/local/gpx/users/user/127.0.0.1:25702/plugins/debug/DebugClass.java:7: error: method does not override or implement a method from a supertype
[14:04:10] [Server thread/WARN]:     @Override
[14:04:10] [Server thread/WARN]:     ^
[14:04:10] [Server thread/WARN]: 3 errors
[14:04:10] [Server thread/ERROR]: null
org.bukkit.command.CommandException: Unhandled exception executing command 'rd' in plugin NowCraftCore v1.0
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-330d66b-fe41b01]
at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-330d66b-fe41b01]
at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:642) ~[spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot.jar:git-Spigot-330d66b-fe41b01]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_25]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_25]
at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:683) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:623) [spigot.jar:git-Spigot-330d66b-fe41b01]
at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:526) [spigot.jar:git-Spigot-330d66b-fe41b01]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_25]

Caused by: java.lang.NullPointerException
at net.nowcraft.core.RuntimeCompiler.RuntimeCompiler.load(RuntimeCompiler.java:127) ~[?:?]
at net.nowcraft.core.RuntimeCompiler.RuntimeCompiler.loadHastebin(RuntimeCompiler.java:88) ~[?:?]
at net.nowcraft.core.RuntimeCompiler.RuntimeCompiler.debugFromHastebin(RuntimeCompiler.java:170) ~[?:?]
at net.nowcraft.core.commands.RuntimeDebug.onCommand(RuntimeDebug.java:35) ~[?:?]
at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-330d66b-fe41b01]
... 14 more

EDIT: I also tested it in a different directory, didn't work. When I use the code on my local windows laptop it does work though.

EDIT 2: It seems to be a problem with CentOS since it's not working on a 2nd CentOS system either.

Joba
  • 777
  • 7
  • 24
  • Can you post the stacktrace? – avgvstvs Dec 22 '14 at 19:00
  • have you verified that the dependent classes are, in fact, findable on the classpath on the system where the compilation failure occurs? additionally, have you verified that the "problem" system has the same version of java that the successful instance has? – him Dec 22 '14 at 19:02
  • @avgvstvs Yes, they do have the same Java version (1.8) and I edited the main post with the stacktrace. – Joba Dec 22 '14 at 20:34
  • I have half a mind to suggest that maybe javac's not able to resolve the network link in the URL you specified. Try using a non-network location for the lib files, ie, just drop them in a `lib` folder relative to the directory you're building from. – avgvstvs Dec 23 '14 at 12:43
  • It is not a network location, it's a directory which is called 127.0.0.1:25702 – Joba Dec 23 '14 at 12:51
  • I also tested it in a different directory, didn't work. When I use the code on my local windows laptop it does work though. – Joba Dec 23 '14 at 15:25

2 Answers2

1

Stacktrace helped immensely, and the surrounding context as well.

The issue here is linux. So the directory you're trying to hit:

/usr/local/gpx/users/user/127.0.0.1:25702/plugins/debug/DebugClass.java

is LITERALLY looking for a directory NAMED 127.0.0.1:25702 (though the : will have to be escaped because most shells use : as a syntax character.

Your network address is going to map to a specific directory. You're going to have to create a network share (Not familiar with CentOS but its usually pretty simple) to eliminate the network reference to a location that physically exists. If you can cd to it from the command line, then your java runtime should be able to actually reference it.

Here is a reference for mounting remote filesystems on CentOS. Yes, I know 127.0.0.1:25702 is technically "localhost" but neither the command shell nor the java runtime is going to know how to resolve a network address in the way you want to do it here. It works on Windows because Windows APIs are more forgiving.

avgvstvs
  • 6,196
  • 6
  • 43
  • 74
0

I'm guessing you are using windows locally and unix for the remote server? the classpath separator for java on a unix machine is ":" and you have a directory that has ":" in the name, the unix compiler may be splitting the directory name by the ":". Did you try using a classpath on the non-working machine that doesn't include a ":" in the file/directory names?

cmparish
  • 66
  • 3
  • Ok, that helped a bit. I compile and execute classes which don't need external libraries (they implement a Debugger interface from my application). I tried using an external library but I'm getting a new error: `Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/MongoClient ` My classpath: `/home/user/DebugTester.jar:/home/user/mongo-java-driver-2.11.2.jar` – Joba Dec 23 '14 at 16:40
  • couple things... 1) try and put a debug statement in buildclasspath to see what its returning. 2) looking at code (not sure haven't tested), but pluginDirectory may have an "/" at the end already.. so adding "/*" may add an extra "/". – cmparish Dec 23 '14 at 17:14
  • It was probably not clear enough, sorry. Check my first response, "My classpath:" is what the method returned. – Joba Dec 23 '14 at 17:19
  • hmm, that is interesting.. and I assume the OS user that the code is running on has read access to to /home/user and the two jar files? – cmparish Dec 23 '14 at 17:42
  • Yes, it does. Do you want the full code so you can test it yourself? – Joba Dec 23 '14 at 18:34
  • This looks like it's a plugin of some kind right? when you say it works from your computer, are you running it as a plugin in the same application you are deploying it to or are you running it in a standalone debugger when you run it locally? – cmparish Dec 24 '14 at 18:47
  • I'm running both in standalone debuggers but it's originally a plugin. This plugin is working perfectly on other servers and my local computer. – Joba Dec 25 '14 at 00:00