【问题标题】:java exec run python program, while python has no authority to I/Ojava exec 运行python程序,而python没有I/O权限
【发布时间】:2023-03-28 11:14:01
【问题描述】:

以下代码在我的本地 PC 中具有 I/O 权限并且可以正常运行。但是,当我尝试在我的 windows server2012 上执行此操作时,出现了一些问题。使用 exec 无法正确运行 python 代码,我的 python 无法给出最终结果。我认为它对 I/O 没有权限,但为什么呢?

Java 代码:

package test;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class test {

        private static void generateFile() throws IOException{
            BufferedWriter bw = new BufferedWriter(new FileWriter("src/test/input.txt", false));
            String eventString = "This is a example";
            bw.write(eventString);
            bw.close();
        }

        private static void getFile() throws IOException{
            BufferedReader br = new BufferedReader(new FileReader("src/test/output.txt"));
            String jsonResults = br.readLine();
            br.close();

        }

        public static void main(String[] args) throws IOException {

            generateFile();

            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("python test.py", null, new File("src/test/"));
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));  
            String line;  
            while ((line = in.readLine()) != null) {  
                System.out.println(line);  
            }  
            in.close();  
            try {
                pr.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                pr.destroy();
            }

            getFile();
        }

    }

python 代码:

f = open('input.txt', 'r')
line  = f.readline()
with open('output.txt', 'w') as fw:
    fw.write(line)

print("Done!")

错误信息:

 Exception in thread "main" java.io.FileNotFoundException: src\test\output.txt (The system cannot find the specified file.)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at test.test.getFile(test.java:21)
    at test.test.main(test.java:49)

【问题讨论】:

    标签: java python python-2.7 cmd exec


    【解决方案1】:

    这里有各种各样的东西(目前更多“代码质量”提示):

    • 你的糟糕方法正在做太多的事情。示例:

    各个“部分”,如下所示 - 都应该进入一个简单的辅助方法:

    String eventString = getJsonEventListInTopic(topicLabel);
    FileWriter fw = ...
    fw.close();
    
    • 含义:你想了解Single Layer Of Abstraction principle
    • core 点在您的实际问题上取得进展:当您在该 Windows 系统上手动运行您的 python 脚本(具有完全相同的参数/参数)时命令行 - 这行得通吗?你知道,这可能是一些超级简单的事情,因为“python.exe 不在你的路径中”

    【讨论】:

    • 首先,感谢您的善意建议。我将分别阅读。然后,我在 Eclipse 上尝试了这段代码,并在我的 PC 上单独使用 cmd 运行 python 脚本
    • 更重要的是,我还在服务器上用 cmd 尝试过这个脚本,它工作正常。
    • 那么您想将堆栈跟踪等所有错误信息添加到您的问题中。
    • 我添加了错误信息。 “恭喜”之前的行表示 python 代码运行正确。但是当继续运行java代码时,提示文件不存在。
    • 另外,目录中确实不存在
    猜你喜欢
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多