Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

eclipse argument

Hi,

I have a program that creates test.txt when i pass test.txt from command prompt as below



java WriteTextFile test.txt

how to run above program by passing test.txt. please advise
i tried run as run coniguration then gave program arguments as test.txt but no luck

please advise
SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please verify if you are running the very same configuration once you have set the parameter.

Check this basic tutorial, I think your program is fine, just that you are not running the configuration which you added param to.

http://theopentutorials.com/tutorials/java/cmd-line-args/how-to-pass-command-line-arguments-in-eclipse-ide/
Can you post the main method of your program?
Avatar of gudii9

ASKER

i will provide this soon
Avatar of gudii9

ASKER

https://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
package com.mkyong;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample1 {

	private static final String FILENAME = "E:\\test\\filename.txt";

	public static void main(String[] args) {

		BufferedWriter bw = null;
		FileWriter fw = null;

		try {

			String content = "This is the content to write into file\n";

			fw = new FileWriter(FILENAME);
			bw = new BufferedWriter(fw);
			bw.write(content);

			System.out.println("Done");

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				if (bw != null)
					bw.close();

				if (fw != null)
					fw.close();

			} catch (IOException ex) {

				ex.printStackTrace();

			}

		}

	}

}

Open in new window


how to pass below as command line argument in eclipse?

E:\\test\\filename.txt

?
please advise
Hi,

You need to change your code to first accept a command line argument and then need to ensure value passed from command line is not null. Later you can pass the variable as suggested above.


                  fw = new FileWriter(FILENAME);

Should become


                  fw = new FileWriter(args[0]);

Even better validate args[0] is not null. Thanks
Avatar of gudii9

ASKER

as attached i passed program argument
package com.mkyong;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample1 {

	private static final String FILENAME = "E:\\test\\filename.txt";

	public static void main(String[] args) {

		BufferedWriter bw = null;
		FileWriter fw = null;

		try {

			String content = "This is the content to write into file\n";

			fw = new FileWriter(FILENAME);
			bw = new BufferedWriter(fw);
			bw.write(content);

			System.out.println("Done");

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				if (bw != null)
					bw.close();

				if (fw != null)
					fw.close();

			} catch (IOException ex) {

				ex.printStackTrace();

			}

		}

	}

}

Open in new window


still did not create. please advise
Because you didn't change line 20 in the above code as I suggested above.

fw = new FileWriter(FILENAME);

Open in new window


change this to

fw = new FileWriter(args[0]);

Open in new window


and then pass the parameter.
Avatar of gudii9

ASKER

package com.gp.maven.calculator.calculator;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample1 {

	//private static final String FILENAME = "C:\\test\\filename.txt";

	public static void main(String[] args) {

		BufferedWriter bw = null;
		FileWriter fw = null;

		try {

			String content = "This is the content to write into000 file\n";

			//fw = new FileWriter(FILENAME);
			fw = new FileWriter(args[0]);
			bw = new BufferedWriter(fw);
			bw.write(content);

			System.out.println("Done");

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				if (bw != null)
					bw.close();

				if (fw != null)
					fw.close();

			} catch (IOException ex) {

				ex.printStackTrace();

			}

		}

	}

}

Open in new window


i changed even then same issue?
Avatar of gudii9

ASKER

under
C:\test

i do not see file888.txt
i passed the argument as well as attached
arg.png
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

ok
Avatar of gudii9

ASKER

from command prompt we never have to give complete path though
That is because the value of File name was hardcoded here:

private static final String FILENAME = "C:\\test\\filename.txt";

Open in new window