0

i have problems creating a temporary file with java through lua. The code works if i start the jav program through the command line but if the program is started by the Lua-plugin it does not create a file.

Cases:

Command Line> java Bot !info

  • BotAnswer.txt will be created in Temp directory if the file already exist it will be overwritten
  • The file contains the correct data

Execution through Lua

  • ERROR: Java programm will start but BotAnswer.txt wont be created ... if file already exists nothing happens

  • The file is missing or contains the wrong data

  • If file already existed it sends the old and wrong content to the chat

I guess there are some permission errors or something like that.

It would be a huge help for me if you could tell me how to fix this.


Here are the code snippets:

located in C:\Program Files\TeamSpeak 3 Client\plugins\lua_plugin\testmodule

Lua

if targetMode == 2 then --targetMode is always 2 for this case
            os.execute("java Bot "  .. message) --Start java program with message as arguments (message = !info)
            if message == "!info" then
                folderName = os.getenv("TEMP")
                fileName = "BotAnswer.txt"
                filePath = io.open(folderName .. "/" .. fileName, "r")
                answer = filePath:read("*all")
                filePath:close()
                os.remove(folderName .. "/" .. fileName)
                ts3.requestSendChannelTextMsg(serverConnectionHandlerID, answer, fromID) --Send the content of BotAnswer.txt to the teamspeak Chat
            end
        end

Java

public class Bot {

    public static void main(String[] args) {
        Bot myBot = new Bot();
        String command = myBot.getCommand(args);
        String answer = myBot.differentiateCommand(command);
        try {
            myProcessor.writeAnswerToFile(answer);          
        } catch (Exception e) {}
    }

    public String getCommand(String[] args) {
        if(args.length == 0) {
            System.exit(0);
        } 
        if (args[0].startsWith("!") != true) {
            System.exit(0);
        }
        String message = args[0];
        if (message.startsWith("!")) {
            String[] msgArray = message.split("!");
            message = msgArray[1];
        }
        return  message;
    }

    public String differentiateCommand(String command) {
        String answer = "";
        if (command.startsWith("info")) {
            answer = "Tis should be the TeamSpeak answer";
        }
    } 

    public void writeAnswerToFile(String answer)throws IOException {
        String tempDir = System.getenv("TEMP");
        File tempFile = new File(tempDir + "/" + "BotAnswer.txt");
        tempFile.createNewFile();
        FileWriter writer = new FileWriter(tempFile);
        writer.write(answer); 
        writer.flush();
        writer.close();
    }

}
  • Probably, your Lua plugin is running under a sandbox and does not allow you full access to file system (creating and writing files). – Egor Skriptunoff Oct 16 '16 at 18:55
  • Or maybe you should use `"\\"` as directory separator instead of `"/"` – Egor Skriptunoff Oct 16 '16 at 19:02
  • Try to execute from Lua plugin the following Lua code to check if you have non-sandboxed os.execute() function: `os.execute[[copy nul: %TEMP%\testfile.txt]]` (it should create file "testfile.txt" on your disk) – Egor Skriptunoff Oct 16 '16 at 19:03
  • @EgorSkriptunoff `os.execute[[copy nul: %TEMP%\testfile.txt]`] created the testfile. Using "\\" in java changed nothing. Do you have any other idea why Java **is** creating the file when its executed by command line but **not** when executed by Lua. – 2w3t1ruh65t4j68541uio02rw Oct 16 '16 at 21:54
  • Every time you open the file, print the file path to the console and check if at some point it prints the wrong file path. – DarkWiiPlayer Oct 17 '16 at 09:32
  • @DarkWiiPlayer I'll try it later because im not home right now but why does it work when executed by cmd line but not with lua? – 2w3t1ruh65t4j68541uio02rw Oct 17 '16 at 13:45
  • Maybe somewhere along the way the environment variable changes? – DarkWiiPlayer Oct 17 '16 at 16:52
  • 1) Check that `folderName .. "/" .. fileName` evaluate to correct filename. 2) Check what processes actually get created with `os.execute` with [Process Monitor](https://technet.microsoft.com/en-us/sysinternals/processmonitor.aspx). Remember, `os.execute` waits only for parent process. If parent process launches other process and exits, `os.execute` returns. 3) Consider an ability creating Lua C++ module for [launching Java within C code](http://stackoverflow.com/questions/819536/how-to-call-java-functions-from-c) for more control. – Vyacheslav Napadovsky Oct 23 '16 at 01:19

0 Answers0