我正在尝试运行另一个主程序时运行主程序。当我使用一个类来执行一个程序时,我遇到了一个io异常,它说......
java.io.IOException: Cannot run program "Test.class": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at Execute.f(Execute.java:43)
at Execute.run(Execute.java:63)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 6 more
我知道该类不是可执行文件。但是为什么在命令提示符下我可以运行程序,但是我可以在程序中运行另一个程序。我不知道我是不是没有正确输入命令提示符。或者我的程序不正确。下面的代码是我试图从另一个主类运行的类。我可以运行像记事本这样的可执行文件和记事本的参数。但我无法运行我创建的另一个主类程序。
import java.io.*;
import java.util.StringTokenizer;
import java.lang.reflect.*;
public class Execute extends Thread{
private String name;
public Execute(String name){
if(name.length() != 0)
{
this.name = name;
}
else
{
System.out.println("No file was entered");
return;
}
}
private void f(){
String[] split = name.split(" ");
File f = new File(split[0]);
for(String k: split)
System.out.println(k);
if(f.isFile())
{
if(f.canExecute())
{
try{
Runtime.getRuntime().exec(split);
System.out.println("program has ran");
}catch(IOException e)
{
e.printStackTrace();
}
}
else if(!f.canExecute())
{
System.out.println(name + " is not an exectuable");
return;
}
}
else
{
System.out.println(name + " Is not a file");
}
}
public void run()
{
f();
}
}
下一课是我尝试运行时出错的一个。
public class Test extends Thread{
public static int n;
public static void main(String[] args){
System.out.println(args[0] + " " + args[1] + " " + args[2]);
n = Integer.parseInt(args[0]);
}
private static int f(int n){
System.out.println("FFFF");
if(n <= 0) return 1;
else return n * f(n-1);
}
public void run()
{
f(n);
}
}
exec Test.java(带参数)和 exec Test.class(带参数)给我同样的错误
这是用于运行exec的主类。
class Main {
public static void main(String[] args) {
while(true){
System.out.print("PROMPT\\>");
String command = "";
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try {
command = stdin.readLine();
}
catch (Exception e) {
System.out.println(e);
return;
}
StringTokenizer tok = new StringTokenizer(command);
if (tok.hasMoreTokens()) {
Token token = new Token(tok.nextToken());
String s = "";
while (tok.hasMoreTokens())
s = s + " " + tok.nextToken();
if (!s.equals(""))
s = s.substring(1);
switch (token.kind) {
case Token.ATTRIB: //done
Thread t1 = new Thread(new Attrib(s)); //done
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.COPY: //done
t1 = new Thread(new Copy(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.DATE: //done
t1 = new Thread(new MyDate());
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.DELETE: //done
t1 = new Thread(new Delete(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.DIR: //done
t1 = new Thread(new Dir(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.EDIT: //done
t1 = new Thread(new Notepad());
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.EXEC: //done
t1 = new Thread(new Execute(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.EXIT: //done
System.exit(1);
case Token.MKDIR: //done
t1 = new Thread(new Mkdir(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.RENAME: //done
t1 = new Thread(new Rename(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.RMDIR: //done
t1 = new Thread(new Rmdir(s));
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.TIME: //done
t1 = new Thread(new MyTime());
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.PRODUCER:
t1 = new Thread(new Producer());
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
break;
case Token.CONSUMER:
t1 = new Thread(new Consumer());
t1.start();
try{t1.join();}
catch(InterruptedException io)
{
io.printStackTrace();
}
default:
System.out.println("Wrong command.");
}
}
}
}
}