java - 如何执行包含来自不同java程序(runtime.exec())的带有某些参数的方法的java类文件?

标签 java class runtime.exec

我正在这样做,这给了我类文件,但我不知道如何执行它,因为我也想在运行时传递参数...

package first;
import java.io.*;           
public class  RuntimeExec {
     public static void main(String[] args) {
           try {
           // print a message
           // create a file with the working directory we wish
           File dir = new File("E:/");
           // create a process and execute notepad.exe and currect environment
           Runtime.getRuntime().exec("javac E:/ImageTest.java");
           Runtime.getRuntime().exec("java E:/ImageTest > E:/out.txt");

           } catch (Exception ex) {
           ex.printStackTrace();
           }
           }
}

我的 ImageTest 类就像在运行时获取文件路径...

public class ImageTest {

private static String DIRECTORY="C:\\Users\\Aashish\\Desktop\\screen";
public static void main(String args[])
{ ImageTest.image(args[0]);
}


public static void image(String FilePath){

try{

    //FilePath="h3.jpg";
    String FinalFilePath=FilePath.substring(0, FilePath.lastIndexOf('.'));
    System.out.println(FinalFilePath);


    int IMG_WIDTH200=200,IMG_HEIGHT200=200;
    BufferedImage resizeImage200x200Png = resizeImage(FilePath,IMG_WIDTH200,IMG_HEIGHT200);
    ImageIO.write(resizeImage200x200Png, "png", new File(DIRECTORY+"\\" + FinalFilePath+ "_"+ String.valueOf(IMG_WIDTH200)+"x"+String.valueOf(IMG_HEIGHT200)+".png"));

    int IMG_WIDTH50=50,IMG_HEIGHT50=50;
    BufferedImage resizeImage50x50Png = resizeImage(FilePath,IMG_WIDTH50,IMG_HEIGHT50);
    ImageIO.write(resizeImage50x50Png, "png", new File(DIRECTORY+"\\" + FinalFilePath+ "_"+ String.valueOf(IMG_WIDTH50)+"x"+String.valueOf(IMG_HEIGHT50)+".png"));

    int IMG_WIDTH500=500,IMG_HEIGHT500=500;
    BufferedImage resizeImage500x500Png = resizeImage(FilePath,IMG_WIDTH500,IMG_HEIGHT500);
    ImageIO.write(resizeImage500x500Png, "png", new File(DIRECTORY+"\\" + FinalFilePath+ "_"+ String.valueOf(IMG_WIDTH500)+"x"+String.valueOf(IMG_HEIGHT500)+".png"));



}catch(IOException e){
    System.out.println(e.getMessage());
}

 }

  private static BufferedImage resizeImage(String FilePath,int IMG_WIDTH,int IMG_HEIGHT ) throws IOException{
    String PATH=DIRECTORY+"\\" + FilePath;
    System.out.println(PATH);

BufferedImage originalImage = ImageIO.read(new File(PATH));
    int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();



BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
}

提前致谢..

最佳答案

使用 ClassLoader 在运行时加载您的类。然后使用refection来调用你的方法;您也可以传递参数。

查看示例(已测试)代码,您可以将代码放在类似的行中:

package examples;
import java.lang.reflect.Method;

public class Refl {

    public static void main(String[] args) throws Exception {

        try {
            Runtime.getRuntime().exec("javac examples/Child.java");

            ClassLoader classLoader = Refl.class.getClassLoader();

            Class<?> aClass = classLoader.loadClass("examples.Child");

            Method method = aClass.getMethod("Add", Integer.class,
                    Integer.class);

            Object returnValue = method.invoke(aClass.newInstance(), 1, 2);

            System.out.println(returnValue);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

package examples;
public class Child {
    public Integer Add(Integer a, Integer b) {
        return a + b;
    }
}

使用的命令:

  • projDir>javac 示例\Refl.java
  • projDir>设置类路径=。
  • projDir>java 示例.Refl
  • 项目目录>3

关于java - 如何执行包含来自不同java程序(runtime.exec())的带有某些参数的方法的java类文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31018902/

相关文章:

java - 为什么要以初始容量启动 ArrayList?

java - 为什么 toString() 不能是静态方法?

java - Runtime.getRuntime().exec(命令);虚拟硬盘无法工作?

Java 优化以防止堆空间内存不足

java - 摘要认证过程中的随机数

java - 使用 BeanIO 将空白字符串解码为 null

c++ - 多个类在多个头文件中互相引用

c++ - 基于c++中的用户输入动态创建类的结构和变量

Java Runtime.getRuntime().exec

java - 如何在 java Runtime.exec 中使用管道