0

I have this simple program that does nothing:

class ShowTheNameOfTheFile{
  static public void main(String[] argv){

  }
}

I compile it as ShowTheNameOfTheFile.jar and configure my Windows machine to open all txt files with it.

Now, when I click a txt file it will open my ShowTheNameOfTheFile.jar program.

In java, how can I know with txt file has opened the ShowTheNameOfTheFile application?

Helio Santos
  • 6,606
  • 3
  • 25
  • 31

2 Answers2

2

You can refer the code by Joop Eggen.

The thing is that if it is the only code and you assign this jar to Windows Open with it might just silently die without printing any output to any console, because it is not started from a console.

First, try from console

java -jar ShowTheNameOfTheFile.jar 1 2 3

if number gets printed then it works okay.

Then add something like this

Process Builder pb = new ProcessBuilder("cmd", "/c", "start", "echo", args[0]);
pb.start();

to your code and then double click a file.

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
1

The command line arguments should give the file paths.

class ShowTheNameOfTheFile{
    static public void main(String[] argv) {
        System.out.println(Arrays.toString(argv));
    }
}
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138