This question already has answers here:
how to add command line parameters when running java code in Eclipse?
(3 answers)
Closed 9 years ago.
public static void main(String[] args) throws FileNotFoundException, SVGFormatException {
SVG svg = new SVG("test.svg"); //so that the file called test.svg can be called upon
PortablePixmap ppm = new PortablePixmap(500, 500); //select the size of the pixmap(canvas)
svg.drawPixmap(ppm); //draw on ppm
ppm.writeToFile("out.ppm"); //save the file as out.ppm file
This is the code I wrote, but I need to get these values from the command line input because if I hard code like this, the user cannot select what values they want to use. Can you please help me how to get these values from command line input?
Click the little arrow next to the Run button and select run configurations -> (x) = arguments tab and below the Program arguments: text box click the variables button and have at it.
The next time you run your program, a dialog box will prompt you for the arguments.
Now, within your program, you can handle these arguments by parsing the String[] args parameter of the main method.
Command-line arguments are already available to you as the String [] passed to your main() method.
So, if you run your program as
java YourProgram test.svg out.ppm
You can access these arguments as
public static void main(String[] args) throws FileNotFoundException, SVGFormatException {
SVG svg = new SVG(args[0]); // test.svg
PortablePixmap ppm = new PortablePixmap(500, 500);
svg.drawPixmap(ppm); //draw on ppm
ppm.writeToFile(args[1]); // out.ppm
}
Since, you're using Eclipse you would have to go to
Right-click > Run As > Run configurations > Arguments tab
Add test.svg out.ppm under Program arguments. Click Apply and then Run.
Related
This question already has answers here:
How to get input via command line in Java? [closed]
(4 answers)
Closed 1 year ago.
this may be blurry to grasp but i'm new to java whole thing and i want to know how can i use cmd to type something in user input
E:\My apps\Java apps\test\src\main\java>java ls.java && echo E:/
enter File name or path :
in this input i want to enter the path via cmd the "&& echo E:/" doesn't work
You can use "Command Line Arguments" in order to give the input when running the program!
Assuming your file name is ls.java
compile it using: javac ls.java
when running it using java command type the arguments in front of it like:
java ls E:/path
Now you can catch the arguments in the program
public class ls {
public static void main(String[] args)
{
String path = args[0];
}
}
you can add more arguments by spaces and catch them in String array
java ls E:/path 100 200
System.out.println(args[1]); // will print 1
System.out.println(args[2]; //will print 200
NOTE: As implied all the command line arguments are stored in the form of String, if you want to convert them to another format, you'd need to parse them using methods.
This question already has answers here:
Eclipse command line arguments
(4 answers)
Closed 5 years ago.
In my CS class we use the terminal to run our codes, using the javac to cimpile and java to run.
In my current assignment I'm receiving inputs from a file. The name of the file is given when inputing the command in the CMD (terminal) like this:
"java -cp mypath\class text.txt"
using this code:
if (args.length != 1) {
final String msg = "Usage: EmployeePay name_of_input file";
System.err.println(msg);
throw new IllegalArgumentException(msg);
}
final String inputFileName = args[0];
final File input = new File (inputFileName);
final Scanner in = new Scanner (input);
When running this with Eclipse, it throws the Exception. Is there a way in eclipse to do this command "java -cp mypath\class text.txt" without changing or adding anything to the code itself?
Go to Run/Run configurations..., then select your "run instance" in the left, then select the Arguments tab and finally enter your text.txt into the Program arguments field. Click Apply and repeat that for the other "run instances" if any.
You may click Run if you want to run your project, but do not forget tto click Apply beforehand...
I made a Java program that you can run :
1 - Only with the GUI by launching the .jar .
2 - With the GUI and a console by launching a .bat .
java -jar "app.jar"
PAUSE
But now I would like to add the possibility to launch it ONLY with the console, that means without the GUI, I searched but didn't found how to do that.
I understand your question like: the application could work console-only, but it starts the ui always.
In that sense, look into you main method /class and simply check for the command line args. Maybe you simply add some -nogui parameter which you then use to put a condition around the launching of any gui components.
And given your comment: you have to compare your args like:
.... void main(String args[]) {
for (String arg : args) {
if (arg.equals(...
See here for more details regarding command line options.
I am still a Java newbie and I have this code. I don't know how to pass the input file to the code. I am using Eclipse Juno.
public static void main(String[] args) {
In in = new In(args[0]); // input file
int N = in.readInt(); // N-by-N percolation system
// turn on animation mode
StdDraw.show(0);
// repeatedly read in sites to open and draw resulting system
Percolation perc = new Percolation(N);
draw(perc, N);
StdDraw.show(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, N);
StdDraw.show(DELAY);
}
}
Whenever I run it I get this exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at PercolationVisualizer.main(PercolationVisualizer.java:42)
What might cause this exception? Could you please be patient with me and explain the process of how to call the input file in the code?
Refer to this guide for adding arguments to your program. Alternatively, you could specify the file name directly in the code instead of reading it from the args.
Basically, the guide instructs the user to go to the Run menu, then "Run..." (actually "Run Configurations..." in recent Eclipse versions), select the appropriate run configuration for the desired project, click the Arguments tab, and enter the arguments (such as file name) in the "Program arguments" section, separated by spaces.
For those using IntelliJ you can set the program arguments via Run->Edit Configuration. Look down about the middle of the window to locate the "Program Arguments" field. Then add the path to the test file and save.
The situation:
Java 6 , Windows XP , Eclipse
I've got an .exe made out ouf an executable .jar.
It is an application which uses a GUI.
The program is supposed to read - or later on write - metadata in images (jpeg ... ).
If the program's open you can load one or more files at once using a filechooser.
So far i'm able to select one (image-) file in my file-system ( windows ), click [Open With ... ] and choose my program - the .exe for opening the selected - single - file.
What I'd like to achieve : Selecting more than one file - for example the whole folder by Ctrl + A and open all the images at once.
In my main method the String[] args does only contain one element no matter how much there have been selected.
The code of the main class:
public class Starter
{
public static void main(String[] args)
{
Exif e = new Exif("Exif ... ");
//try{ Thread.sleep(1000); }catch(Exception ex){}
e.init();
e.setSize(1024,700);
e.setSize(1024,600);
// The array is handled by a gui-class which reads the Strings from the array
// and opens the referenced files one by one ...
// Like :
// for(String filename : args
// { open(filename); }
e.open(args);
}
}
Did you try selecting the files that you want and dragging them on top of that exe?