How to open a file when clicked on a java application?

Asked

Viewed 843 times

1

I made an application in Java and I want to open a certain type of file in it, like with Netbeans, that is when a person clicks on the file ". java " it opens in Netbeans( or another IDE that accepts this type of file).
However, even if I configure the file to open in my application, when it is opened, it does not show its contents in my application. Are there any java commands to do this ? To+

EDITED :

It’s all over the place, but I’m not doing it right. I know how to open a file ( I already have a method for this), but I can’t call it in passing the path because it gives error.

follows the code:

// essa é uma das minhas classes ,é ela que é chamada e possui todos os componentes visuais
public static void main(  String args[]) {

public void run() {
                new Programa().setVisible(true);
                  if(args.length > 0) {
                        try {
                      MetodoAbir(args[0])





            }catch(Exception erro) {

            }

        }

But the error says that the method can not be "refenrenciated by Static contex". This occurs even if I put a method without parameter, ie I can not call the methods there. I also tried to create a global variable and play the value args[0] in it to pass to the method in another location but also gives the same error.

  • Do you want to set your program as default in windows, or do you want to run some application within it? It is not clear that in question.

  • I set it as default. I want to run something inside it, but when I click on the file the application opens but does not show the contents of the file.

  • You are probably getting the file path as parameter, but you are not doing anything with it.

  • 2

    Add the code responsible for this execution by clicking EDIT on the question.

  • How so, in the code I have to do a check to know if someone externally clicked on a file when opening the application ? If yes, how do I do it ?

  • @diegofm , that’s what I’m wondering how to check if any file was externally clicked to open it in the application. I only know how to do it using jfilechooser .

  • What have you done so far? Add that to the question, your question we already know, now you need to show where you want to put it and how is the current code where you want to add this feature.

Show 2 more comments

1 answer

0

When opening the application you have to check if parameters have not been passed to your program.

import java.io.File;

public class SeuPrograma {
    public static void main(String[] args) {
        if(args.length > 0) {
            File file = new File(args[0]);

            // Abra seu arquivo
        }
    }
}
  • The main thing you didn’t say, which is how to open the file.

  • I think the main thing is to be aware that you have a file arriving via the command line. How to open depends on the logic of his program which I do not know what it is. It can be a text file, a zip, an image, etc.

  • But the doubt is just "How to open a file when clicked on a java application" and the answer did not explain any of this. Here is the problem in answering questions whose doubt was not clear enough.

  • Ugly issue in question.

  • Now I’ve done it right. When you call the main class step as parameter args for a constructor that receives this parameter from the other class and then do the process to open. It’s all right, but still nothing appears when I click to open a file. It’s like I haven’t passed anything when I click on a file.

  • Display a message with the parameters your program is receiving and see what happens.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.