例如在文件夹下有若干.class文件
求教如何在jsp中显示这些.class文件
并选择其一运行之?

解决方案 »

  1.   

    显示应该就是文件的操作吧?运行的话我打算在servlet里用类似下面的代码实现
    String [] arguments=new String[]{"-classpath","d:\\","d:\\"+filename};
    javac.compile(arguments);
    String result=filename.substring(0,filename.indexOf('.'));
    Process child=Runtime.getRuntime().exec("java -classpath d:\\ "+result);
    System.out.println("java -classpath d:\\ "+result);就是显示并选择的过渡代码不知道怎么写
    求教
      

  2.   

    to xlyyc:
    差不多 但有点区别 动态加载要用到reflect
    我这里直接用相当于手动编译的代码简化了
    String [] arguments=new String[]{"-classpath","d:\\","d:\\"+filename};
    javac.compile(arguments);
    String result=filename.substring(0,filename.indexOf('.'));
    Process child=Runtime.getRuntime().exec("java -classpath d:\\ "+result);
    System.out.println("java -classpath d:\\ "+result);现在要做的就是从某个文件夹里得到.class文件名的String就可以了
      

  3.   

    现在要做的就是从某个文件夹里得到.class文件名的String就可以了--------------如果知道具体文件夹的位置,可以用File dir = new File("e:/cfaq");
    File[] fs = dir.listFiles();

    for (int i = 0; i < fs.length; i++) {
    FileReader fr = new FileReader(fs[i]);
            fs[i].getName(); //得到文件名
    ....
    }
      

  4.   

    to jimshen:
    谢谢
    我试了下你的代码 
    报错:
    FileList.java:13: unreported exception java.io.FileNotFoundException; must be ca
    ught or declared to be thrown
                          FileReader fr = new FileReader(fs[i]);我的测试代码如下:
    import java.io.*;
    public class FileList   
    {
    public static void main(String[] args) 
    {
    File dir = new File("f:/mambo");
    File[] fs = dir.listFiles();
    int i ;
    for (i = 0; i < fs.length; i++) {
    FileReader fr = new FileReader(fs[i]);
    fs[i].getName(); //得到文件名
    } System.out.println(fs[i]);
    }
    }
    有什么地方不对吗 请教
      

  5.   

    public static void main(String[] args) throws Exception或者用try...catch进行异常处理
      

  6.   

    楼上的 main函数可以throws exception吗。
    如果这么写了,就不是main函数了吧。。
      

  7.   

    main不能抛出异常吗?一样可以的!stoneagecr(stoneagecr)你的System.out.println(fs[i]);写到循环外边去了,会出错import java.io.*;
    public class FileList
    {
    public static void main(String[] args) throws Exception
    {
    File dir = new File("f:/mambo");
    File[] fs = dir.listFiles();
    int i ;
    for (i = 0; i < fs.length; i++) {
    FileReader fr = new FileReader(fs[i]);
    System.out.println(fs[i].getName()); //输出文件名
    }
    }
    }