Running Java classes by dragging and dropping windows

I have a java class file with the main method. On Windows, I would like to be able to drag and drop files onto the desktop icon / short / etc, which would cause the supply of file names to my main method. Basically, I want to allow users to drag and drop files when the program runs, rather than entering them on the command line.

Any thoughts?

+5
source share
5 answers

To build the answer on daub815, on Windows you can use a batch file to pass arguments to another command. In this case, we will use the launcher javato launch your class using the method main.

Google , , , . , :

@ECHO OFF
:Loop
IF "%1" == "" GOTO Done
java YourClass %1
SHIFT
GOTO Loop
:Done

( ".bat" ), , .

, .

. , , . , , , , , , . - , , , . wiki.

+4

, ... - DropHandler UUID . , :

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.class]
@="JavaClass"

[HKEY_CLASSES_ROOT\JavaClass\DefaultIcon]
@="C:\\Java\\jdk1.6.0_05\\bin\\java.exe,1"

[HKEY_CLASSES_ROOT\JavaClass\shell\open]
@="Run Java class"

[HKEY_CLASSES_ROOT\JavaClass\shell\open\command]
@="\"C:\\Java\\jdk1.6.0_05\\bin\\java.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\JavaClass\shellex\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"

... !
, java.exe , ! .

, , - script, , / ( , !). , , . WSH, Windows. JS script, , VB script.

, script (LaunchJavaClass.js):

if (WScript.Arguments.count() == 0)
{
  WScript.StdOut.Write("No parameters");
  WScript.Quit(1);
}
var className = WScript.Arguments.Item(0);
//~ WScript.StdOut.Write(className + "\n");
var m = className.match(/^(.*)\\(.+?)\.class$/);
if (m == null)
{
  WScript.StdOut.Write("Not a class file");
  WScript.Quit(1);
}
var classPath = m[1];
className = m[2];
//~ WScript.StdOut.Write(classPath + " >>> " + className + "\n");
var params = new Array();
for (i = 1; i < WScript.Arguments.count(); i++)
{
  params[params.length] = WScript.Arguments.Item(i);
}
var cmd = "cmd /c cd /D " + classPath + 
    " & C:/Java/jdk1.6.0_05/bin/java.exe " + 
    className + " " + params.join(" ");
//~ WScript.StdOut.Write(cmd + "\n");
var shell = WScript.CreateObject("WScript.Shell");
//~ var exec = shell.Exec(cmd); // Can be used to get stdout
shell.Run(cmd, 0);

, , ( cscript).
, JRE .

command , :

[HKEY_CLASSES_ROOT\JavaClass\shell\open\command]
@="\wscript -b "D:\\_PhiLhoSoft\\WSH\\LaunchJavaClass.js\" %1 %*"

, .

, .class, main().

import java.io.*;

class TestDnD
{
 public static void main(String[] args)
 {
  Writer output = null;
  try
  {
   output = new BufferedWriter(new FileWriter(new File("LogFile.txt")));
   for (String arg : args)
   {
    output.write(arg + "\n");
   }
  }
  catch (IOException ioe)
  {
   ioe.printStackTrace();
   return;
  }
  finally
  {
    try { output.close(); } catch (IOException e) {}
  }
 }
}

, .reg - , . drag'n'drop .jar ( , ).

: Java! , . . , -Djava.ext.dirs="some path;another path", - ( jar).

+2

PhiLho , JAR ( , ) .reg , . .reg, , . JAR , , Drag and Drop.

Remember to change the path to the installation location of the Java executable.

Windows Registry Editor Version 5.00 

[HKEY_CLASSES_ROOT\.jar] 
@="jarfile" 

[HKEY_CLASSES_ROOT\jarfile\DefaultIcon] 
@="C:\\Java\\jdk1.7.0\\bin\\java.exe,1" 

[HKEY_CLASSES_ROOT\jarfile\shell\open] 
@="Run Java Program" 

[HKEY_CLASSES_ROOT\jarfile\shell\open\command] 
@="\"C:\\Java\\jdk1.7.0\\bin\\java.exe\" -jar \"%1\" %*" 

[HKEY_CLASSES_ROOT\jarfile\shellex\DropHandler] 
@="{86C86720-42A0-1069-A2E8-08002B30309D}" 
+2
source

Adding to Adiel A. If you create a batch file that launches your Java window using Swing. You would have to delete the files in this window. Then you can skip these files.

0
source

So, is there no way for windows to pass args to main () themselves via drag and drop?

0
source

All Articles