newbie start java program with switches
Hi Guys,
Im working on an assignment for my school and for example i need to be able to excute my program from DOS adding switches in for example.
java Main -e password input.txt output.text
or
java Main -d password input.txt output.text
so depending on the switches the program will run differntly
The program i am creating(Main.java) will encrypt(-e) or decrpt(-d) a text file based on the password(password). it will have an input text file (input.txt) with infomation in clear text and an output text file which will have the infomation encrypted (output.text).
Im very rusty with my java programming and i have a lot to learn and catch up but i dont know how i can achive this can you please help me.
I am using netbeans to create my program with
Re: newbie start java program with switches
please can someone point me in the right direction
Re: newbie start java program with switches
You can do it like this:
Code Java:
public class ProgramArguments {
/**
* JavaProgrammingForums.com
*/
public static void main(String[] args) {
if(args.length < 1){
System.out.println("No argument");
}
else if(args[0].equals("-d")){
System.out.println("Decrypt Method");
}
else if (args[0].equals("-e")){
System.out.println("Encrypt Method");
}
}
}
This is just an example.
java -jar ProgramArguments.jar -d
args[1] password
args[2] input.txt
args[3] output.txt
Re: newbie start java program with switches
ok say for instance i wanted to check if the password part of the starting argument was happy would i look in args[1] for example
Code Java:
if (args[1].equals("password1234")){
veriffy password ect ect
}
becasue i need to run the password through an algorythum to verify if it is correct or not
and the text files would be ars [2] abd args[3]
also do you know how i can use netbeans to run the java application wth the arguments ???
EDIT: I FOUND OUT HOW Netbeans IDE Blog by Tushar Joshi, Nagpur: Using Command Line Arguments in NetBeans IDE
Re: newbie start java program with switches
Yes args[1] would be the password string :)
Glad you found out how to run command line arguments with Netbeans.
If you ever use Eclipse, you can do the same thing like this:
http://www.javaprogrammingforums.com...s-eclipse.html