Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何执行包含来自不同java程序(runtime.exec())的带有参数的方法的java类文件?_Java_Class_Runtime.exec - Fatal编程技术网

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

如何执行包含来自不同java程序(runtime.exec())的带有参数的方法的java类文件?,java,class,runtime.exec,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

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

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;
}
}

提前感谢。

使用类加载器在运行时加载类。然后使用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;
    }
}
包示例;
导入java.lang.reflect.Method;
公共类Refl{
公共静态void main(字符串[]args)引发异常{
试一试{
Runtime.getRuntime().exec(“javac examples/Child.java”);
ClassLoader ClassLoader=Refl.class.getClassLoader();
类aClass=classLoader.loadClass(“examples.Child”);
Method=aClass.getMethod(“Add”,Integer.class,
整数类);
Object returnValue=method.invoke(aClass.newInstance(),1,2);
系统输出打印项次(返回值);
}catch(classnotfounde异常){
e、 printStackTrace();
}
}
}
包装示例;
公营儿童{
公共整数加法(整数a、整数b){
返回a+b;
}
}
使用的命令:

  • projDir>javac示例\Refl.java
  • projDir>设置类路径=
  • projDir>java examples.Refl
  • projDir>3

使用
ProcessBuilder
开始和上次检查时,重定向不适用于
Process
此外,您需要通过
ImageTest
要处理的图像的完全限定路径/名称。您应该使用
Process#getInputStream
来处理执行的输出,只需在类文件名之后立即传递参数,即Runtime.getRuntime().exec(“java e:/ImageTest\“\”>e:/out.txt”);为什么要使用java来运行命令?这段java代码看起来没有做任何有用的事情,只是妨碍了运行shell命令:从命令行执行所有操作!