import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;public class Compile {
private String mess = null;
private File file;
private Runtime run;
private Process pro;
public static void main(String[] args) throws InterruptedException{
Compile com = new Compile();
com.setFile(new File("D:\\Test\\Java23"));
System.out.println(com.isCompile());
} public Compile() {
this.run = Runtime.getRuntime();
} public void setFile(File file) {
this.file = file;
} public String isCompile() throws InterruptedException {
try {
System.out.println(this.file.getPath());
this.pro = this.run.exec("cmd.exe /c javac " + this.file.getPath() + ".java");
BufferedReader buf = new BufferedReader(new InputStreamReader(this.pro.getErrorStream()));
String str;
while ((str = buf.readLine()) != null) {
mess += str + "\n";
} if (null == this.mess) {
this.mess = "编译成功";
}
return mess;
} catch (IOException ex) {
return ex.getMessage();
}
}
}
这是一个编译.java文件的程序(可运行)。得到.class文件后请问怎么调用windows自带的java指令使之运行。有结果的并将之输出到控制台上来!!!!求解答。
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;public class Compile {
private String mess = null;
private File file;
private Runtime run;
private Process pro;
public static void main(String[] args) throws InterruptedException{
Compile com = new Compile();
com.setFile(new File("D:\\Test\\Java23"));
System.out.println(com.isCompile());
} public Compile() {
this.run = Runtime.getRuntime();
} public void setFile(File file) {
this.file = file;
} public String isCompile() throws InterruptedException {
try {
System.out.println(this.file.getPath());
this.pro = this.run.exec("cmd.exe /c javac " + this.file.getPath() + ".java");
BufferedReader buf = new BufferedReader(new InputStreamReader(this.pro.getErrorStream()));
String str;
while ((str = buf.readLine()) != null) {
mess += str + "\n";
} if (null == this.mess) {
this.mess = "编译成功";
}
return mess;
} catch (IOException ex) {
return ex.getMessage();
}
}
}
这是一个编译.java文件的程序(可运行)。得到.class文件后请问怎么调用windows自带的java指令使之运行。有结果的并将之输出到控制台上来!!!!求解答。
this.run.exec("cmd.exe /c javac " + this.file.getPath() + ".java");
这句的道理一样啊,
用命令行运行
this.run.exec("java " + this.file.getPath());
控制台上敲:java Compile
即可。
我随手写的,你可能要把this.run.getOutputStream()这个包装一下。
因为System.out是一个PrintStream。
然后再敲d: 你要把它放在桌面 然后敲javac yy.java(这名字你自己取) 桌面上就有一个class文件 然后敲java yy 就行了
try{
//执行命令
pro = runtime.exec("cmd.exe /c javac "+"D:\\Test\\Java23.java");
//取得命令结果的输出流
InputStream fis = pro.getInputStream();
//用一个读输出流类去读
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
//逐行读取输出到控制台
while ((line = br.readLine()) != null) {
System.out.println(line);
}
我糊了,到底是output还是input~~~~~~~~~~·
上面写错了,应该是cmd.exe /c java 不是javac。我现在要弄运行
这句应该这么写么。。
Exception in thread "main"
我自己用开始-》cmd然后java Java23就可以运行出结果。说明我是这句话("cmd.exe /c java "+"D:\\Test\\Java23.java"); 写错了。谁能告诉我错在哪里了。
java命令是用来执行.class文件的,你让java命令执行一个源文件算是怎么回事?
java.lang.NoClassDefFoundError: D:\Test\Java23/java
它把点号变成了路径分隔符,很明显是把Java23.java作为一个class文件名了。所以它认为是java23文件夹下的java.class文件。