以exe为扩展名的文件称为可执行文件。在windows中可通过资源管理器查找要运行的可执行文件,然后双击该文件。
在java程序中可通过文件对话框查找要运行的可执行文件,然后通过Runtime类的exec方法运行该文件。下面是一个例子。
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class smp extends Frame implements ActionListener
{
public static void main(String args[])
{
smp frm=new smp();
}
FileDialog op;
Button btn1,by=tn2;
smp()
{
super("运行科执行文件。");
setLayout(null);
setBackground(Color.cyan);
setsize(300,200);
setVisible(true);
btn1=new Button("运行");
btn2=new Button("关闭");
btn1.setBounds(40,70,50,30);
btn2.setBounds(120,70,50,30);
op=new Filedialog(this,"运行",FileDialog.load);
add(btn1);add(btn2);
btn1.addActionListener(this);
btn2.addActionerlistener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getsource==btn1)
{
Stirng str;
op.setVisible(true);
try
{
File fl=new File(op.getdirectory(),op.getFile());
Runtime rt=Runtime.getRuntime();
rt.exec(fl.toString());
}
catch(FileNotFoundException e)
{
system.out.println("File not found");
}
if(e.getsource()==btn2)
{
System.exit(0);
}
}
}