英文:
InMemoryJavaCompiler cannot compile file
问题
我试图使用InMemoryJavaCompiler编译一个.java文件,但是我遇到了一个错误。
我的代码:
try {
final var path = Paths.get("C:/EffectHandler.java");
final var pathToStr = Files.readString(path, StandardCharsets.UTF_8);
System.out.println(pathToStr);
InMemoryJavaCompiler.newInstance().compile("EffectHandler", pathToStr);
}
catch (final Exception e) {
e.printStackTrace();
}
我的错误:
[kind=ERROR, line=24, message=class main is public, should be declared in a file named main.java]
at com.l2jserver.gameserver.scripting.InMemoryJavaCompiler.compileAll(InMemoryJavaCompiler.java:125)
at com.l2jserver.gameserver.scripting.InMemoryJavaCompiler.compile(InMemoryJavaCompiler.java:146)
at com.l2jserver.TEST.main(TEST.java:44)
我的C文件:
public class main
{
public static void main(final String[] args)
{
System.out.println("已编译");
}
}
该文件名为EffectHandler.java。
英文:
I'm trying to compile a .java file with InMemoryJavaCompiler
but i get an error.
My code:
try {
final var path = Paths.get("C:/EffectHandler.java");
final var pathToStr = Files.readString(path, StandardCharsets.UTF_8);
System.out.println(pathToStr);
InMemoryJavaCompiler.newInstance().compile("EffectHandler", pathToStr);
}
catch (final Exception e) {
e.printStackTrace();
}
My error:
[kind=ERROR, line=24, message=class main is public, should be declared in a file named main.java]
at com.l2jserver.gameserver.scripting.InMemoryJavaCompiler.compileAll(InMemoryJavaCompiler.java:125)
at com.l2jserver.gameserver.scripting.InMemoryJavaCompiler.compile(InMemoryJavaCompiler.java:146)
at com.l2jserver.TEST.main(TEST.java:44)
My file in C:
public class main
{
public static void main(final String[] args)
{
System.out.println("Compiled");
}
}
The file is named EffectHandler.java
答案1
得分: 1
问题以及解决方案都明确写在错误消息中。
类main
需要位于自己的文件中 - main.java
。
或者,如果你不想更改文件名,将类重命名为EffectHandler
。
英文:
The problem, as well as its solution, are explicitly written in the error message.
The class main
needs to be in its own file - main.java
.
Alternatively, if you don't want to change the filename, rename the class to EffectHandler
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论