Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i implement opening a file with my java program?

I have a basic, simple and maybe stupid question, but how do I implement that I can drag a file onto my java program and open it? I searched really long for this basic question.....

As long as I found out you can't implement dragging it onto the .jar, because its not executable. You have to create a .exe, which also open your .jar, but that's all! I would really like to know how :)

A keyword would be enough, if I can get the answer through searching this keyword.

Thanks, Leander

//Edit: I may have expressed things a little bit complicated. Later i want to have a shortcut on, for example, the desktop where i can drag any file on the shortcut and the programm opens with the file(it will, at this point) only move it to a special location.

I don´t know how the code for this would be, I even don't know how to google for this (I only get questions how to implement "open with" with the answer Desktop.open(File f)).

like image 545
Leander Avatar asked Oct 08 '22 16:10

Leander


1 Answers

For windows only:

option 1:

make a batch file into the directory of your jar file (or anywhere you like but then you need to adjust the path, you can also make a shortcut from the batch file).

@ECHO OFF
start java -jar %~dp0MYAPP.jar %1

option 2:

  • Make a shortcut (right click - new - shortcut)
  • Enter into the location "java -jar C:\path\to\myapp.jar"

drag and drop will work if your jar accepts filename as a parameter

public class Main {

    public static void main(String[] args) {
        if (args.length == 0)
            System.out.println("No arguments");
        else
            System.out.println("1st argument: " + args[0]);
    }

}
like image 115
Imre L Avatar answered Oct 13 '22 10:10

Imre L