在内存中的Java编译器无法编译文件。

huangapple go评论80阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年10月2日 02:41:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64161424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定