英文:
File always empty when trying to save it from a shell script that is executed via java Process
问题
使用Java的Process,我试图使用gpg解密文件,并将其保存为.txt文件到我的本地文件系统。如果我从终端执行此脚本,它会正确执行。但当我通过Java进程执行它时,它会在项目根目录下创建文件,但文件总是空的。我没有收到任何错误信息,所以很难诊断。
sh脚本:
gpg --batch --yes --passphrase-file pass --import private-key.asc
gpg --batch --yes --passphrase-file pass -d fileStillEncrypted.txt.pgp > decrypted_file.txt
上面的第一行有点多余,因为它只需要执行一次,但暂时保留在那里。
我将文件权限更改为777进行测试,只是为了查看是否是权限问题:
-rwxrwxrwx 1 user 767211335 6037 Aug 10 11:24 fileStillEncrypted.txt.pgp
-rw-r--r--@ 1 user 767211335 2667 Aug 10 11:36 private-key.asc
-rwxrwxrwx 1 user 767211335 141 Aug 21 08:33 decryptFile.bat
-rwxrwxrwx 1 user 767211335 178 Aug 21 09:08 decryptFile.sh
"pass"文件包含密码,位于与sh脚本相同的目录中。
Java代码:
public void decryptFile() {
Process p;
try {
String[] cmd = {"sh", "/Users/user/path/to/decryptFile.sh"};
p = Runtime.getRuntime().exec(cmd);
p.waitFor();
} catch (IOException e) {
// TODO catch exception properly
e.printStackTrace();
} catch (InterruptedException e) {
// TODO catch exception properly
e.printStackTrace();
}
}
任何帮助都将不胜感激,即使只是关于如何调试Process的建议,因为我过去没有使用过它。
英文:
Using java Process, I'm trying to decrypt a file with gpg and save it as a .txt file to my local filesystem. If I execute this script from my terminal it acts correctly. When I execute it through the java process it creates the file on the root of my project but it is always empty. I don't get any errors so it's making it hard to diagnose.
The sh script:
gpg --batch --yes --passphrase-file pass --import private-key.asc
gpg --batch --yes --passphrase-file pass -d fileStillEncrypted.txt.pgp > decrypted_file.txt
The first line above is a little redundant as it only needs to be done once but left in there for now.
I changed the file permissions to 777 for testing just to see if it was a permissions issue:
-rwxrwxrwx 1 user 767211335 6037 Aug 10 11:24 fileStillEncrypted.txt.pgp
-rw-r--r--@ 1 user 767211335 2667 Aug 10 11:36 private-key.asc
-rwxrwxrwx 1 user 767211335 141 Aug 21 08:33 decryptFile.bat
-rwxrwxrwx 1 user 767211335 178 Aug 21 09:08 decryptFile.sh
The "pass" file contains the pw and is in the same directory as the sh script.
The Java code:
public void decryptFile() {
Process p;
try {
String[] cmd = {"sh", "/Users/user/path/to/decryptFile.sh"};
p = Runtime.getRuntime().exec(cmd);
p.waitFor();
} catch (IOException e) {
// TODO catch exception properly
e.printStackTrace();
} catch (InterruptedException e) {
// TODO catch exception properly
e.printStackTrace();
}
}
Any help would be appreciated even if it's just advice on how to debug Process as I haven't used it in the past.
答案1
得分: 1
太棒了。查看标准错误后,我发现尽管我正在使用路径命中我的shell脚本,但它并未在该路径中查找我在其中引用的文件,而是在我的Java应用程序的根路径中查找。我将文件移动到那里,然后它就找到了这些文件。起初,我确实在文件中看到了“不适当的ioctl”错误。我通过更新gnu中的以下配置并重新启动,发现文件会被正确地填充。
在./.gnupg中:
创建了gpg-agent.conf和gpg.conf文件
gpg.conf文件内容:
use-agent
pinentry-mode loopback
gpg-agent.conf文件内容:
allow-loopback-pinentry
然后需要使用以下命令重新启动gnu进程:
➜ .gnupg echo RELOADAGENT | gpg-connect-agent
OK
英文:
Awesome. Getting the standard error showed me that although Im hitting my shell script with the path it ISN'T looking for the files I reference in that path but in the root path of my Java application. I moved the files there and it picked them up. I did see in the file "inappropriate ioctl" error at first. I found that by updating the following configurations in gnu and restarting that the file would populate correctly.
in ./.gnupg:
created gpg-agent.conf and gpg.conf
gpg.conf:
use-agent
pinentry-mode loopback
gpg-agent.conf:
allow-loopback-pinentry
then needed to restart the gnu process with:
➜ .gnupg echo RELOADAGENT | gpg-connect-agent
OK
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论