0

I am coding a JAVA reader to read .txt files as a substitution for notepad. Now, the problem is, I want to set that program to be the default program to open all .txt files. Now, how the program will distinguish which file I am opening via the program? I don't want to make a program that will first open JFileChooser and will make the user to open that file via JFileChooser. I just want to make a notepad in java that will open .txt files like notepad does. When I will click on a file say, *.txt, it will use my program to open that file and my program will show the chars of that file.

iMagur
  • 721
  • 1
  • 6
  • 11
  • It should just be a matter of making your program accept a filename as its first parameter. – ScoPi Dec 05 '12 at 15:22

2 Answers2

1

Surely the issue here is making Windows/OSX/Linux register your program as the default which presumably has to happen outside the confines of your program?

You can do an "Open with..." on Windows/Mac (not sure on Linux) and just use your program instead. Your program then could take the file name as the command line argument and do with it what you want.

Smalltown2k
  • 811
  • 8
  • 13
  • command line argument! I knew it!! :D does it act like C++ in JAVA? If its the same then the problem is over! – iMagur Dec 05 '12 at 15:26
  • Can't Executable Jar File be set to open a file for default program in Windows 7? It says it is not a valid Win32 application? – iMagur Dec 05 '12 at 15:37
  • You should be able to find a program to convert it to an EXE file which would work. It's just not Java out-of-the-box as it would break the "write once, run anywhere" paradigm. Try here: http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file – Smalltown2k Dec 05 '12 at 15:39
1

I didn't test it, but I would expect that if you create a Jar file from your sources, and you let your OS point to that file to open *.txt files, then the absolute path of the file to open would be in your main's "String[] args". Can you make a quick test?

Jonathan H
  • 7,591
  • 5
  • 47
  • 80
  • I don't know how to use command line argument in JAVA, if its like C++, I am testing it right now! – iMagur Dec 05 '12 at 15:26
  • The command line arguments are merely these "args" in your `public static void main(String [] args)` :) – Jonathan H Dec 05 '12 at 15:29
  • Can't Executable Jar File be set to open a file for default program in Windows 7? It says it is not a valid Win32 application... Help me plz! – iMagur Dec 05 '12 at 15:38
  • Check out the last comment of Smalltown2k to turn it into an exe file – Jonathan H Dec 05 '12 at 15:40
  • this is a very small program I have written in JAVA to experiment (http://paste.ubuntu.com/1412703/), then compiled and run via netbeans. Then I opened a file of random extension, and set the default program to open that file to the Executable JAR file from the project which is a program, then when I try to open the file it shows the error is not a valid Win32 application. What to do? – iMagur Dec 05 '12 at 15:44
  • Turn your Jar file into an exe using this: http://www.jar2exe.com/ (cf. http://stackoverflow.com/questions/1973431/how-can-i-make-an-exe-in-netbeans) – Jonathan H Dec 05 '12 at 15:55
  • Np, please mark one answer as a solution so others can benefit from your question too ;) – Jonathan H Dec 05 '12 at 18:49