指定一下dir: Process exec(String[] cmdarray, String[] envp, File dir) Executes the specified command and arguments in a separate process with the specified environment and working directory. 或: Process exec(String command, String[] envp, File dir) Executes the specified string command in a separate process with the specified environment and working directory.vb生成的1.txt可能在其他目录(java执行的当前目录,而不是vb程序所在的目录)了,试试指定一下dir到vb程序所在的目录。
Process exec(String[] cmdarray, String[] envp, File dir)
Executes the specified command and arguments in a separate process with the specified environment and working directory.
或:
Process exec(String command, String[] envp, File dir)
Executes the specified string command in a separate process with the specified environment and working directory.vb生成的1.txt可能在其他目录(java执行的当前目录,而不是vb程序所在的目录)了,试试指定一下dir到vb程序所在的目录。
楼主找找看,到底是哪种原因:
1.exec方法的参数错误。
请楼主认真读读API,如果楼主参数中有路径,或者路径名中带空格、汉字、特殊符号什么的,
要采用那个多个参数的重名方法来调用。
2.由于当前路径不同,生成文件的路径可能发生变化。
这是最常发生的问题,楼主分析一下VB程序,看看生成文件和当前路径有无关系,查找一下看看。
3.调用exec方法后,没有等待VB程序执行完毕就将Java程序先结束了。
JVM是一个进程,调用VB,操作系统会分配一个子基础来运行VB程序,JVM是父进程。
当父进程结束时,子基础也会被回收。
楼主除了调用exec方法,还应该调用其返回值对象的一个方法来等待VB进程的结束。
第三个也好解决。
关键是第一个,这个要认真读读API,选用合适的同名方法,才可以。
我觉得是java虚拟机的问题 上面的方法是创建了一个java程序的子进程,确实有运行程序,但是不能实际对系统造成修改 用这个方法运行记事本然后保存文件就可以发现保存的文件不见了最后我用ProcessBuilder实现了 这个是直接执行而不是变成子进程