0

I want to use cmd Commands in java program,

Want to crope all images in folder, I downlaoded ImageMagick, and using cmd commands Its working 1 image,

cd C:\Users\Robert\Java-workspace\Crop_test\Crop_test1
cd  convert -crop 312x312+0-10 image1.jpg new_image1.jpg

But, I want to use this in Java so, I can crop all images in folder by program, Here is my java program:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.omg.CORBA.portable.OutputStream;

 public class test1 {
    public static void main(String argv[]) throws IOException, InterruptedException {

        ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "C:\\Users\\Robert\\Java-workspace\\Crop_test\\Crop_test1\\", "convert -crop 312x312+0-10 image1.jpg new_image1.jpg"); 
        Process p = pb.start();
        p.waitFor();
    }
}
7
  • I suspect that "convert -crop 312x312+0-10 image1.jpg new_image1.jpg" should be separated parameters. Also it seems that directory should not be part of command. Maybe add it later using pb.directory(new File("put path here")) Commented Jan 12, 2014 at 14:35
  • Like, ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "convert", "-crop 312x312+0-10", "image1.jpg","new_image1.jpg"); pb.directory(new File("C:\\Users\\Robert\\Java-workspace\\Crop_test\\Crop_test1\\")); Please help me!! thanks Commented Jan 12, 2014 at 14:42
  • Something like that. Does it work? If not then maybe try to print error stream of p Process and share what error you get. Commented Jan 12, 2014 at 14:45
  • I tried but not working, Thanks for your help!! Commented Jan 12, 2014 at 14:46
  • Similar question: stackoverflow.com/q/15464111 Commented Jan 12, 2014 at 14:50

3 Answers 3

2

Although you are asking how to use CMD and this was addressed on other answers I think that the best solution (considering your explanation of your implementation) would be to use a ImageMagick wrapper for Java as you can see here.

Cheers

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help guys found the solution!!
0

You can invoke CMD commands as follows in Java;

Runtime.getRuntime().exec(your_command);

Best thing for you to do is to creat a batch file with the commands you need to run and then invoke your batch file using the following command;

Runtime.getRuntime().exec("cmd /C start D:\\test.bat");

because you cannot do any change directory commands using the Runtime class. Please try this option and let me know if you face any other issues.

10 Comments

You can also use ProcessBuilder, and the questioner already knows this. This answer does not help at all.
Yes you can, but why add complexity when you can achieve the same with minimal code?
Thank you so much for your help, I tried that same thing!!Please help me thanks!!
@dinukadev : ProcessBuilder internally does the same thing... It provides an extra layer of abstraction, that's all... And an extra layer of abstraction means extra safety and simpler usage...
byt using Runtime.getRuntime().exec("C:\\Users\\Robert\\Java-workspace\\R_Java_Map\\Map_Image\\", "convert -crop 312x312+0-10 image1.jpg new_image1.jpg"); Like This?? Please help me!! Thanks
|
0

A couple of things to try (both of which are untested):

  1. Put a cd in the command line and use && to run both commands in one line.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import org.omg.CORBA.portable.OutputStream;
    
     public class test1 {
        public static void main(String argv[]) throws IOException, InterruptedException {
    
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cd C:\\Users\\Robert\\Java-workspace\\Crop_test\\Crop_test1\\ && convert -crop 312x312+0-10 image1.jpg new_image1.jpg"); 
            pb.redirectErrorStream(true);
            Process p = pb.start();
            p.waitFor();
        }
    }
    
  2. Change the directory that the ProcessBuilder starts in:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import org.omg.CORBA.portable.OutputStream;
    
     public class test1 {
        public static void main(String argv[]) throws IOException, InterruptedException {
    
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "convert -crop 312x312+0-10 image1.jpg new_image1.jpg");
            pb.directory(new File("C:\\Users\\Robert\\Java-workspace\\Crop_test\\Crop_test1\\"));
            pb.redirectErrorStream(true);
            Process p = pb.start();
            p.waitFor();
        }
    }
    

Incidentally, are you sure you want to import org.omg.CORBA.portable.OutputStream? Did you mean java.io.OutputStream instead?

EDIT: if things still aren't working, then the next step is to see whether the problem is that convert isn't being found. Let's just run convert on its own without any arguments and see if it spits out its usage message to standard output. Run the following:

public class test1 {
    public static void main(String argv[]) throws IOException, InterruptedException {

        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "convert");
        pb.redirectErrorStream(true);
        Process p = pb.start();
        StreamGobbler g = new StreamGobbler(p.getInputStream(), "OUT");
        g.start();
        p.waitFor();
    }
}

Use the StreamGobbler class here. Does this print out convert's usage method, with each line prefixed with OUT>?

10 Comments

ohh ya java.io.OutputStream yap,Thanks you so much for your help, I tried, still not working!!Please help me, Thanks!!
@user3187463 not working is not good explanation of problem. What do you mean by that? Do you get any errors? Does it work different that you expected? Does it not affect anything?
I am sorry, I tried your code, its not giving me any error, but, I check in folder, it didn't convert or create new image.. Please help me!! thanks!!
What do you mean by 'not working'? Which approaches did you try, and what was the outcome? Were there any error messages? Did the code hang? Have you tried connecting a StreamGobbler to the output stream? Is the return code returned by waitFor zero or not?
I tried first one, It didn't give me any error, nothing print out, I checked folder old image is in folder but, it didn't create new image in that folder, Please help me!! Thanks in advanced!!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.