-3

I have text file named data.text which I want to pass as argument to my program named program.java . from the command line after compilation I just use

$java program data ----

how could I pass the same argument to the program in eclipse IDE. I guess it is possible to pass the argument to program in eclipse and run it

eskoba
  • 532
  • 1
  • 7
  • 25
  • 1
    Why is this tagged C++ if you're asking about arguments to a *Java* program? – Borgleader Jul 22 '13 at 17:44
  • 4
    Your seach on SO should have brought you some answers like http://stackoverflow.com/questions/12222153/eclipse-how-we-take-arguments-for-main-when-run?rq=1 or this one here: http://stackoverflow.com/questions/13694123/how-can-i-configure-eclipse-to-run-my-java-program-with-extra-arguments?rq=1 – ConcurrentHashMap Jul 22 '13 at 17:45

5 Answers5

5

This eclipse help page explains it quite well.

bowmore
  • 10,842
  • 1
  • 35
  • 43
2

If you have:

import java.util.Arrays;
public class Program {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(args));
    }
}

When you run the program without config:

[]

In Run > Run configurations...

enter image description here

The output for this:

[ABC.txt, a b c.txt, OK]
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
1

In Eclipse you can set up a Run Configuration for the Java Application. Click on the green "play" button in the Launch toolbar (next to the bug icon which starts debugging).

Within that configuration, you can set the working directory and command line arguments - and even prompt the user for command line arguments when it's run, using arguments like ${string_prompt:Foo}.

The menu you are looking for

from a post by Jon Skeet

Community
  • 1
  • 1
Stephan
  • 16,509
  • 7
  • 35
  • 61
  • 1
    Why are you copy and pasting an existing answer? – Brian Roach Jul 22 '13 at 17:49
  • @BrianRoach because OP wants an answer and is too lazy to do any kind of research as you can clearly tell – Stephan Jul 22 '13 at 17:50
  • 1
    Stephan has credited the answer, so why the downvote? Not being rhetorical here: I understand duplication flagging (used it myself a couple of times) but is copying content and crediting it so bad that it deserves a downvote? – Mena Jul 22 '13 at 17:56
  • @Mena thats what I was thinking – Stephan Jul 22 '13 at 17:57
  • 2
    I didn't downvote (though was on the fence), but the point was that you flag it as a dup, not repost someone else's answer. – Brian Roach Jul 22 '13 at 18:00
  • @BrianRoach I understand why that's advisable and I guess I'll do both, but I like to pretend that this website is about helping people, even if they dont know how to google. – Stephan Jul 22 '13 at 18:02
1

Not sure if this will help, but if your app has a main class with a main method, this snippet of code will take care of handling the first argument and creating a file from it:

public static void main(String[] args) {
    if (args != null && args.length > 0 && args[0] != null && !args[0].trim().isEmpty()) {
        File file = new File(args[0]);
        if (file.exists() && file.canRead()) {
            // TODO logic
        }
        else {
            // TODO crash gracefully
        }
    }
    else {
        // TODO crash gracefully
    }
}

If your question was only about how to pass arguments to a program with Eclipse, then please refer to bowmore's answer.

Mena
  • 47,782
  • 11
  • 87
  • 106
1

Click Run, Run Configurations, and then choose (x)=Arguments. In Program arguments you can write the argument that you want to use.

Lavinia
  • 11
  • 2