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

1
  • 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? Commented Feb 15, 2012 at 14:09

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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!
Thank you so much, this works now, and I now understand that specifying a directory provides a working folder! Thank you!
0

I hope here you can find some code examples and solutions

1 Comment

Thank you, I'll have a look at those links now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.