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();
    }
}
Petr Abdulin
  • 30,380
  • 8
  • 56
  • 90
  • 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"))` – Pshemo Jan 12 '14 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 – user3187463 Jan 12 '14 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. – Pshemo Jan 12 '14 at 14:45
  • I tried but not working, Thanks for your help!! – user3187463 Jan 12 '14 at 14:46
  • Similar question: http://stackoverflow.com/q/15464111/ – Luke Woodward Jan 12 '14 at 14:50
  • `import java.io.OutputStream;` was originally intended (though not used here). – Joop Eggen Jan 12 '14 at 14:53
  • Thank you so much for your help guys found the solution!! – user3187463 Jan 12 '14 at 19:04

3 Answers3

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

prmottajr
  • 1,804
  • 1
  • 12
  • 22
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.

dinukadev
  • 2,149
  • 14
  • 22
  • 3
    You can also use ProcessBuilder, and the questioner already knows this. This answer does not help at all. – Luke Woodward Jan 12 '14 at 14:42
  • Yes you can, but why add complexity when you can achieve the same with minimal code? – dinukadev Jan 12 '14 at 14:44
  • Thank you so much for your help, I tried that same thing!!Please help me thanks!! – user3187463 Jan 12 '14 at 14:45
  • 2
    @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... – TheLostMind Jan 12 '14 at 14:45
  • 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 – user3187463 Jan 12 '14 at 14:50
  • @user3187463 i have edited my answer. Please check this option. – dinukadev Jan 12 '14 at 15:21
  • Thank for your answer but, I want to crop all images in folder, not only one image soo.. is there anyway, I can use that command in cmd and than create bat file use this line.. that you so much for your solution.. or I just need to enter cd line in bat file and from java i can enter convert line?? Please help me!! Thanks!! – user3187463 Jan 12 '14 at 15:26
  • can you try as follows.. Runtime.getRuntime().exec("cmd /C cd \"C:\\Users\\Robert\\Java-workspace\\R_Java_Map\\Map_Image\" && convert -crop 312x312+0-10 image1.jpg new_image1.jpg"); – dinukadev Jan 12 '14 at 15:32
  • I tried but same thing, it didn't convert image.. please help me!! Thanks!! – user3187463 Jan 12 '14 at 16:02
  • Thank you so much for your help guys found the solution!! – user3187463 Jan 12 '14 at 19:05
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>?

Luke Woodward
  • 56,377
  • 16
  • 76
  • 100
  • ohh ya java.io.OutputStream yap,Thanks you so much for your help, I tried, still not working!!Please help me, Thanks!! – user3187463 Jan 12 '14 at 14:53
  • @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? – Pshemo Jan 12 '14 at 14:56
  • 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!! – user3187463 Jan 12 '14 at 14:57
  • 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](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html?page=2) to the output stream? Is the return code returned by `waitFor` zero or not? – Luke Woodward Jan 12 '14 at 14:58
  • 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!! – user3187463 Jan 12 '14 at 15:01
  • @user3187463: you haven't answered all of my questions. What was the exit code returned by `waitFor()`? What output do you get if you use a StreamGobbler, as previously linked to? Also, please note that I've added the line `pb.redirectErrorStream(true);` to both code blocks above. That redirects standard error into standard output, so that you don't have to read from standard error and standard output separately. – Luke Woodward Jan 12 '14 at 15:07
  • @user3187463: the next thing to try is to see if you can run `convert` on its own without any arguments and without setting the directory. See my updated answer. – Luke Woodward Jan 12 '14 at 15:33
  • I did try it, but.. please help me!! Thank you so much!! – user3187463 Jan 12 '14 at 15:43
  • But... but... what? I went to the effort of writing out some code that I thought might help determine where the problem is, and all you can say is 'I did try it, but...', and leave me guessing at what actually happened. **By not answering the questions I'm asking you you are making it very difficult to help you.** – Luke Woodward Jan 12 '14 at 16:01
  • Thank you so much for your help guys found the solution!! – user3187463 Jan 12 '14 at 19:06