Wednesday, 15 April 2015

java - How to run program from command line? -



java - How to run program from command line? -

i trying write random numbers file. 1 number on each line , method name takes filepath argument , long n argument number of random numbers generated. have far:

public void generate(long n, string filepath) throws filenotfoundexception { printwriter out = new printwriter("filepath"); random r = new random(); for(int = 0; < n; i++) { int num = r.nextint(10); out.write(num + "\n"); } out.close(); }

am on right track? how go running programme via cmd. know commands compiling , running think need tester run this. give me instructions on how go running code via cmd.

edit: got working! help!

you're close printing. instead of doing

out.write(num + "\n");

youll instead want do

out.println(num);

which cross platform (and in opinion, easier read).

additionally, in order run programme command line, you'll need add together main method in class, so

public static void main(string[] args) throws filenotfoundexception { int n = integer.parseint(args[0]); string filepath = args[1]; yourclass c = new yourclass(); c.generate(n, filepath); }

this main assumes you're passing in 2 parameters command line, number followed filename

hope helpful

java command-line

No comments:

Post a Comment