2

Very basic command-line related question:

I have never tried to run anything in command line from java before and am struggling with the basics - other online information doesn't seem to work for my example, or I'm not understanding it.

In command line this is what it looks like:

C:\gnuplot\binary>gnuplot 15FebPlotFile.gp

All I have to do in command line is navigate to the correct file location (C:\gnuplot\binary) and then type gnuplot 15FebPlotFile.gp and it runs the thing I need (which simply generates a PDF and saves it to that file location)

I've seen people use Runtime and Process like on this site http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html but I don't understand how I call the various command, like cd C:\gnuplot\binary and then from that location get it to run gnuplot 15FebPlotFile.gp.

If anyone could give me any advice on an approriate site to look at or some lines of code that might help me I'd be really greatful.

Thank you

Lindsay
  • 23
  • 3
  • is it OK for your application to pass the full path of the "15FebPlotFile.gp" file as a command? or to change the file's path? – Ahmad Y. Saleh Feb 15 '12 at 14:09

2 Answers2

2

You can work with ProcessBuilder, and then you can set the working directory of the process using ProcessBuilder#directory(File dir):

ProcessBuilder processBuilder = new ProcessBuilder("gnuplot", "15FebPlotFile.gp");
processBuilder.directory(new File("C:\\gnuplot\\binary"));
Process p = processBuilder.start();
MByD
  • 135,866
  • 28
  • 264
  • 277
  • This looks great!! I'm still having a problem though that it says that "C:\\gnuplot\\binary" is of the wrong type, String not File.. Is there a way to cast it to a File somehow? Thank you! – Lindsay Feb 15 '12 at 14:15
  • Thank you so much, this works now, and I now understand that specifying a directory provides a working folder! Thank you! – Lindsay Feb 18 '12 at 13:02
0

I hope here you can find some code examples and solutions

Community
  • 1
  • 1
JackBauer
  • 135
  • 1
  • 3
  • 10