Running code in Eclipse with additional input [duplicate] - java

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...

Related

Java - how can i type in user input using cmd command [duplicate]

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.

Launch JAR from vba script with JOptionPane

Are there any limitations in launching an executable JAR from a VBA script using shell(...)
My jar effectivly gets some IDs from the VBA script as launch arguments, queries values from a web service and displays them using JOptionPane.ShowMessageDialog
Here is my code:
private static JFrame quotenframe = new JFrame();
public static void main(String args[]){
if(args.length < 3){
JOptionPane.showMessageDialog(quotenframe, "Not enough parameters!", "Error", JOptionPane.ERROR_MESSAGE);
closeAll(1);
}
if(args[0].split("#").length<2){
JOptionPane.showMessageDialog(quotenframe, "Invalid value! '#' missing", "Error", JOptionPane.ERROR_MESSAGE);
closeAll(1);
}
String var1 = args[0].split("#")[0];
String var2 = args[0].split("#")[1];
String var3 = args[1];
String var4 = args[2];
String result = "";
// Build resultString
JOptionPane.showMessageDialog(quotenframe, result);
closeAll(0);
}
The JAR itselfs executes without a problem when launched from a windows cmd shell, but when that same command line is run from the Shell(...) command in my VBA script, the only reaction is a java icon visible for a split second in the task bar, which then disappears.
My command is:
java -jar jarFolder\myjar.jar param1 param2 param3
and the execution directory is one level ontop of jarFolder.
It seems to me, that the JAR crashes upon launch, but, I cannot see why, as that same JAR, in the same directory, launched with the same command from a Windows shell works well.
Can it have anything to do with the JOptionPane? If not, any idea what the error could be?
The solution is, that you need to specify the full path to the jar, when invoking it using VBA's Shell(...) command, if the JAR is in a subfolder.

Sending command line arguments from Eclipse [duplicate]

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.

How to input a file in this code?

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.

Working with file in Eclipse

I have instruction to run program in command line, for example:
java SetTest < alice30.txt
I wonder how to do this in Eclipse. I tried to put this in Run Configuration like this:
Another thing I don't know is where to put this file (alice30.txt). Is this in root of project or in src folder where source files are located?
I know these are beginner questions but I am stuck and need help.
EDIT:
As #Kane suggested I passed File and opened stream.
Instead of:
Scanner in = new Scanner(System.in);
I now use:
Scanner in = new Scanner(new File("alice30.txt"));
You can pass full file path in arguments (e.g. c:/.../alice30.txt))
The eclipse root directory is the base directory of the project (i.e., not the src/ directory, directly under the project.)
It's generally good style to have a 'resources' folder for txt, graphics, etc.
Rather than trying to pass a stream you could just pass the filename and open the stream yourself.
The reason what you're doing in Eclipse isn't working is because your command prompt/shell/dos/bash/whatever is handling creating the input stream out of the file for you. Eclipse doesn't do this. So, from the command line: < alice.txt means "run this program with no arguments, and create a stream to system.in", while doing that in Eclipse means "run this program with two arguments '<' and 'alice.txt'
you need do like this:
add:
import java.io.IOException;
import java.nio.file.Paths;
then:
replace"Scanner in = new Scanner(System.in);"to"Scanner in =new Scanner(Paths.get("alice30.txt"));" .
and you also need do this : "public static void main(String args[]) throws IOException "
With information from this link/page and several tries, I figure out a way to pass argument and file using the local route in eclipse Run -> Run Configurations.. , though it is not recommended as Kane said.
For my case: I need to do " $java someClass tinyW.txt < tinyT.txt " (This is an example from Algorithms book by Robert Sedgewick)
In my case, " tinyW.txt " is a argument, so in the eclipse environment, you can set in Run -> Run Configurations -> Arguments -> Program arguments: /local address/tinyW.txt. For my Ubuntu: /home/****/tinyW.txt
" < tinyT.txt " is a file that pipe to the main arguments, so you can set the route and file in " Run -> RUn Configurations -> Common ", click the "Input File", use the File System icon and select the file from local compute. See the figure. So in Input File: /local_address/tinyT.txt. My case is: /home/***/tinyT.txt. Hope it also works for you.

Categories