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();
    }

}
4

0 回答 0