Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.io.File not working #342

Closed
Losin6450 opened this issue May 18, 2024 · 11 comments · Fixed by #352
Closed

java.io.File not working #342

Losin6450 opened this issue May 18, 2024 · 11 comments · Fixed by #352
Assignees
Labels
bug Something isn't working

Comments

@Losin6450
Copy link

When using a node instance the java.io.File class doesn't work

@Losin6450
Copy link
Author

its specifically the File.exists() method that doesn't work

@caoccao
Copy link
Owner

caoccao commented May 18, 2024

Please leave more info for the troubleshooting. OS, CPU arch, source code, etc.

@Losin6450
Copy link
Author

its for a Minecraft plugin

public class Grakkit extends JavaPlugin implements CommandExecutor {

    V8Runtime runtime;
    JavetJVMInterceptor interceptor;
    @Override
    public void onEnable() {
        try {
            saveDefaultConfig();
            reloadConfig();
            String main = getConfig().getString("main", "index.js");
            String mode = getConfig().getString("mode", "js");
            if (mode.equalsIgnoreCase("node")){
                runtime = V8Host.getNodeInstance().createV8Runtime();
            } else {
                runtime = V8Host.getV8Instance().createV8Runtime();
            }
            runtime.setConverter(new JavetProxyConverter());
            interceptor = new JavetJVMInterceptor(runtime);
            interceptor.register(runtime.getGlobalObject());
            runtime.setV8ModuleResolver(new IV8ModuleResolver() {

                private Path DEFAULT = getDataFolder().toPath();
                private Path LAST = getDataFolder().toPath();

                @Override
                public IV8Module resolve(V8Runtime v8Runtime, String file, IV8Module iv8Module) throws JavetException {
                    try {
                        String filename = file.endsWith(".js") ? file : file + ".js";
                        Path f = LAST.resolve(filename);
                        if (Files.exists(f)) {
                            LAST = f.getParent();
                            V8Module module = v8Runtime.getExecutor(Files.lines(f).collect(Collectors.joining("\n"))).setResourceName(f.toFile().getAbsolutePath()).compileV8Module();
                            return module;
                        } else {
                            LAST = DEFAULT;

                            f = LAST.resolve(filename);
                            if (Files.exists(f)) {
                                LAST = f.getParent();
                                V8Module module = v8Runtime.getExecutor(Files.lines(f).collect(Collectors.joining("\n"))).setResourceName(f.toFile().getAbsolutePath()).compileV8Module();
                                return module;
                            }
                            return null;
                        }
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                    return null;
                }
            });
            runtime.allowEval(true);
            runtime.getGlobalObject().set("Class", Class.class);
            runtime.getGlobalObject().set("Logger", getLogger());
            File file = new File(getDataFolder(), main);
            if (!file.exists()){
                file.getParentFile().mkdirs();
                file.createNewFile();
            }
            V8StringExecutor stringExecutor = (V8StringExecutor) runtime.getExecutor(Files.lines(file.toPath()).collect(Collectors.joining("\n")));
            stringExecutor.setModule(true);
            stringExecutor.setResourceName(file.getAbsolutePath());
            stringExecutor.execute();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onDisable() {
        if (runtime != null){
            try {
                runtime.await();
                interceptor.unregister(runtime.getGlobalObject());
                runtime.close();
                System.gc();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

}
``` when using normal v8 runtime there is no issues but when using node i get this error when trying to use java.io.File 

java.io.IOException: Den angivne sti blev ikke fundet
at java.io.WinNTFileSystem.createFileExclusively(Native Method) ~[?:1.8.0_412-412]
at java.io.File.createTempFile(File.java:2063) ~[?:1.8.0_412-412]
at net.minecraft.server.v1_16_R2.Convertable$ConversionSession.a(Convertable.java:303) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]
at net.minecraft.server.v1_16_R2.WorldServer.save(WorldServer.java:768) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]
at net.minecraft.server.v1_16_R2.MinecraftServer.saveChunks(MinecraftServer.java:674) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]
at net.minecraft.server.v1_16_R2.MinecraftServer.stop(MinecraftServer.java:741) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]
at net.minecraft.server.v1_16_R2.DedicatedServer.stop(DedicatedServer.java:647) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]
at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:887) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]
at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-b5a13e6-0d8350a]

@caoccao
Copy link
Owner

caoccao commented May 18, 2024

It seems to be a permission issue?

@Losin6450
Copy link
Author

Losin6450 commented May 18, 2024

It only happens when using the node runtime, doesn't happen when using v8 runtime

@caoccao
Copy link
Owner

caoccao commented May 18, 2024

Well, I don't play Minecraft. Here are some ideas for your reference.

  • Create a regular Java app with the same code, deploy that app to the same env to see if things break.
  • Call File API in Java to see if it works.

@AsyncProxy
Copy link

使用节点实例时,java.io.File 类不起作用

If you write Spigot/Fabric/Forge plugins or mods, you can embed the Rhino engine, otherwise the plugins will be very large, and my suggestion is to use them more widely for embedded apps.

@AsyncProxy
Copy link

好吧,我不玩 Minecraft。以下是一些想法供您参考。

  • 使用相同的代码创建一个常规的 Java 应用程序,将该应用程序部署到同一个环境,看看是否有问题。
  • 在 Java 中调用 API 以查看它是否有效。File

Pls what happened to the problem that the engine of 3.1.2 does not work, I still can't run, no code has been changed, 3.1.1 is working normally.

@Losin6450
Copy link
Author

after alot of trial and error the issue seems to come from the setResourceName method on the executor

@caoccao
Copy link
Owner

caoccao commented May 25, 2024

You may workaround it if it causes issues at your end. Please refer to this commit for detail.

@caoccao
Copy link
Owner

caoccao commented May 30, 2024

It's now surrounded by a try-catch clause so that that error can be omitted and logged.

@caoccao caoccao added the bug Something isn't working label May 30, 2024
@caoccao caoccao self-assigned this May 30, 2024
@caoccao caoccao linked a pull request Jun 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants