00:00
ok great, so can you open the file from the command line by running javaw.exe -jar "C:\...\mjar.jar" "C:\...\file.myext"
ok wait, when you say "when I open up a file from the application" do you mean in your Java app, you click File > Open, and choose a file from the Open dialog?
Ok now, what I want to you to tell me is, how does the File > Open command handle the whole "open the file and display its contents" procedure?
Ok, now, here's the thing. When you click File > Open and choose a file, the filename gets stored as a String in the userFile variable. right?
This is what you have to do. In the main method, set the userFile variable to args[0] and run everything in the Open command starting from BufferedReader(FileReader(userFile));
I suggest you put all of that in a separate method which accepts a String argument, then use that method in both the method handling File > Open and the main method.
Ok, currently, is this roughly how your File > Open handling method works? When the user clicks File > Open, this method handles the event: { Display the "Open File" dialog to the user. Get the string 'userFile' from the dialog. BufferedReader(FileReader(userFile)); while { ((line=b.readLine())!=null) doc.insertString(doc.getLength(), "ignore", style); } }
When the user clicks File > Open, this method handles the event: { Display the "Open File" dialog to the user. Get the string 'userFile' from the dialog. myMethod(userFile); } Write another method "myMethod (String userFile)": { BufferedReader(FileReader(userFile)); while { ((line=b.readLine())!=null) doc.insertString(doc.getLength(), "ignore", style); } }
public static void main (String args[]) { set up your GUI and everything here first myMethod(args[0]); //this will open the incoming file and display its contents }
BTW all my pseudocode above is under the assumption that inputs are always valid. In your actual code, you have to include various checks. In method handling File > Open, you'll have to check if user cancelled the Open dialog.
Off-topic: Eclipse seems to favor (String[] args) maybe that's why I'm used to it. Not sure which one Netbeans favors.
Oh, and please try File > Open even if the args[0] failed. You'll want to make sure myMethod works correctly.
Btw, does anything in your new myMethod depend on anything else? For example, does it have to wait for something to be setup, something that will surely be setup when user does File > Open, but may not be set up when the main method is executing on program start?
Actually I wanted you to split out the actual opening of file into myMethod so that you can re-use it in the main method.
public static void main (String args[]) { set up your GUI and everything here first myMethod(args[0]); //this will open the incoming file }
2 hours later…
02:47
Here is the problem, the args array will have zero length if there are no arguments. You will need to have proper checks for the length of the array. Please try these in your main method (along with the necessary GUI stuff that would come before this code):
public static void main (String[] args) { System.out.println ("args length = " + args.length); for (int i=0; i<args.length; i++) { System.out.println(args[i]); } if (args.length == 1) { myMethod(args[0]); } }
19 hours later…
« first day (2 days earlier) ← previous day next day → last day (15 days later) »