iovxw

使用 Clojure 为 Minecraft 编写 Bukkit 插件时出现 Could not locate clojure/core__init.class or clojure/core.clj on classpath 的解决方法

标题太长短不了

09:10:17 [SEVERE] Could not load 'plugins/watershipdown-0.1.jar' in folder 'plugins': 
java.lang.ExceptionInInitializerError
	at clojure.lang.Namespace.<init>(Namespace.java:34)
	at clojure.lang.Namespace.findOrCreate(Namespace.java:176)
	at clojure.lang.Var.internPrivate(Var.java:149)
	at net.jonnay.watershipdown.WatershipDown.<clinit>(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:247)
	at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:131)
	at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:285)
	at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:200)
	at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:156)
	at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:132)
	at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigurationManager.java:52)
	at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:148)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:407)
	at net.minecraft.server.ThreadServerApplication.run(SourceFile:465)
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: Could not locate clojure/core__init.class or clojure/core.clj on classpath: 
	at clojure.lang.Util.runtimeException(Util.java:165)
	at clojure.lang.RT.<clinit>(RT.java:319)
	... 15 more
Caused by: java.io.FileNotFoundException: Could not locate clojure/core__init.class or clojure/core.clj on classpath: 
	at clojure.lang.RT.load(RT.java:430)
	at clojure.lang.RT.load(RT.java:398)
	at clojure.lang.RT.doInit(RT.java:434)
	at clojure.lang.RT.<clinit>(RT.java:316)
	... 15 more

比如上面这样的报错,是因为 Clojure 默认使用当前线程 context 的 Classloader 来加载 class 的

如果 context 没有设置,Clojure 就挂了

所以加载 Clojure 之前设置一下就好了

Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());

为了以后方便,魔改了一个前置插件:

https://github.com/iovxw/cljminecraft

加了这个插件以后,改一下所写插件的 plugin.yml, 加入 depend: [cljminecraft],让服务端先加载这个前置插件

(记得编译的之前把 project.clj 里的 Bukkit API 改成需要的版本)