I am doing like this which gives me class file but i don't know how to execute it as i want to pass argument also at run time...

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

My ImageTest class is like which takes filepath at runtime...

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

Thanks in advance..

1

There are 1 answers

0
Rajesh On

Use ClassLoader to load your class at runtime. Then use refection to invoke your methods; you can pass arguments as well.

See sample (Tested) code, you can fit your code on similar lines:

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

Commands Used:

  • projDir>javac examples\Refl.java
  • projDir>set classpath=.
  • projDir>java examples.Refl
  • projDir>3