How can I implement opening a file using my java program?

I have a simple, simple and possibly stupid question, but how can I implement this, can I drag the file into my java program and open it? I have been looking for this basic question for a very long time .....

As long as I found out, you cannot implement dragging it onto .jar because it is not executable. You have to create a .exe that will also open your .jar, but that’s it! I would really like to know how :)

A keyword will be enough if I can get an answer by searching for that keyword.

Thanks Leander

// Editing: Perhaps I complicated the situation a bit. Later, I want to have a shortcut on, for example, on the desktop, where I can drag any file to the shortcut, and the program will open with the file (it will be at that moment) only move it to a special place.

I don’t know what the code for this will look like, I don’t even know how to do it for Google (I have questions about how to implement "open with" with the answer Desktop.open (File f)).

+4
source share
3 answers

The place to start reading is the JDK 6 drag and drop documentation . Or you can start right with “Drag and Drop”. .

If you want to create a desktop icon that will open your application when you put the file on it, it depends on the OS. I think Launch4j will support this, although I did not use it that way.

0
source

For windows only:

option 1:

make a batch file in 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 shortcut (right click - new - shortcut)
  • Enter in the field "java -jar C: \ path \ to \ myapp.jar"

drag and drop will work if your jar accepts the file name as 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]); } } 
+4
source

In the window you can create a .bat file, and if you Dnd in this file, you will get the file name as the fisrt parameter

therefore the code

 echo %1 pause 

writes the file name, so you can run a java program, for example

 java -jar myApp.jar MyAppClass %1 
0
source

Source: https://habr.com/ru/post/1415863/


All Articles