简体   繁体   English

通过 Netbeans 8.2(Java 项目)运行的 Octave 脚本没有创建文件(编辑:根本没有运行)

[英]Octave script run through Netbeans 8.2 (Java Project) is not creating files (edit: not running at all)

I ran an Octave script through Java's ProcessBuilder and Process classes.我通过 Java 的 ProcessBuilder 和 Process 类运行了一个 Octave 脚本。 I used Netbeans and the Script file is in resources folder of the Project I am doing.我使用了 Netbeans,脚本文件位于我正在做的项目的资源文件夹中。

When run from bash directly the script is working fine.当直接从 bash 运行时,脚本工作正常。 When run from Java, I'm pretty sure the script is working fine.从 Java 运行时,我很确定脚本运行良好。

In the below program,在下面的程序中,

#!/usr/bin/octave -qf
function ret = manipulateCell(x)

    x = x/max(x(:));
    x = x.*255;
    x = int32(x);
    a1 = mean(x(:));

    ret = a1<70;
end

img = imread('aaa.png');
imgInd = rgb2ind(img);
imgGray = ind2gray(imgInd,colormap());

sizeVector = 100*ones(1,20);


Cells =  mat2cell(imgGray,sizeVector,sizeVector);

ManipCells = cellfun(@manipulateCell,Cells);

file2D = fopen('data.txt','rw+');
dlmwrite(file2D,ManipCells);

Last two lines seem not to run from Netbeans;最后两行似乎不是从 Netbeans 运行的; there is no output (ie, no output file is created).没有输出(即,没有创建输出文件)。

Java Code which is used to run this script.用于运行此脚本的 Java 代码。

ProcessBuilder pb = new ProcessBuilder("src/resources/ProcessImg.m");
try{
    Process p = pb.start();
}
catch(IOException ex){
    ex.printStackTrace();
}

-- Edit: - 编辑:

I have just tried imshow(img) in between the above code.我刚刚在上面的代码之间尝试了imshow(img) It didn't work either.它也没有用。


Edit:编辑:

How I verified that ProcessBuilder and Process work fine?我如何验证 ProcessBuilder 和 Process 工作正常? and How do I know which directory I am in.以及我如何知道我在哪个目录中。

String command = "pwd";
ProcessBuilder pb = new ProcessBuilder(command);
        //pb.directory(new File("./"));

        try{
        Process p = pb.start();

        //Debug Code
        pb.redirectErrorStream(true);
        BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String s;

        while((s=bf.readLine())!=null){
            System.out.println(s);
        }

        p.getInputStream().close();
        p.getOutputStream().close();
        p.getErrorStream().close();
        }
        catch(IOException ex){
            ex.printStackTrace();
        }

If the command string is pwd , the output is如果command字符串为pwd ,则输出为

/home/user/NetBeansProjects/Project

If the command string is ls , the output is如果command字符串为ls ,则输出为

build
build.xml
manifest.mf
nbproject
src
test

I want to say that commands are being executed.我想说的是命令正在执行。

Also, process builder is identifying shebang notation.此外,流程构建器正在识别 shebang 符号。


The problem is NetBeans is not allowing ProcessImg.m to create files in it's directory by an external process probably.问题是 NetBeans 可能不允许 ProcessImg.m 通过外部进程在其目录中创建文件。


#!/usr/bin/octave -qf
function ret = manipulateCell(x)

    x = x/max(x(:));
    x = x.*255;
    x = int32(x);
    a1 = mean(x(:));

    ret = a1<70;
end

img = imread('~/Desktop/aaa.png');

imgInd = rgb2ind(img);
imgGray = ind2gray(imgInd,colormap());

sizeVector = 100*ones(1,20);


Cells =  mat2cell(imgGray,sizeVector,sizeVector);

ManipCells = cellfun(@manipulateCell,Cells);

file2D = fopen('~/Desktop/data.txt','rw+');
dlmwrite(file2D,ManipCells);

I have put absolute path (from home) as suggested and it didn't work.我已经按照建议放置了绝对路径(从家里),但它没有用。 Not only in NetBeans did the file hasn't been created but also on desktop.不仅在 NetBeans 中没有创建文件,而且在桌面上也没有创建。

I'm pretty sure that "ProcessBuilder" doesn't understand the Shebang in your script (or you haven't set it executable) and you have to call it like我很确定“ProcessBuilder”不理解脚本中的 Shebang(或者你没有将它设置为可执行),你必须像这样调用它

ProcessBuilder pb = new ProcessBuilder("/usr/bin/octave",
                                       "src/resources/ProcessImg.m");

But this looks like a common problem so I suggest searching for "ProcessBuilder execute bash script" (or perl script) which gives many hits.但这看起来是一个常见的问题,所以我建议搜索“ProcessBuilder 执行 bash 脚本”(或 perl 脚本),它提供了很多点击。

Also try an absolute path to your script and explicitely close your file on exit.还可以尝试使用脚本的绝对路径,并在退出时明确关闭文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM