Код: Выделить всё
package com.sun.jna.platform.win32;
import com.sun.jna.WString;
import com.sun.jna.platform.FileUtils;
import java.io.File;
import java.io.IOException;
public class W32FileUtils extends FileUtils {
public W32FileUtils() {
}
public boolean hasTrash() {
return true;
}
public void moveToTrash(File[] files) throws IOException {
Shell32 shell = Shell32.INSTANCE;
ShellAPI.SHFILEOPSTRUCT fileop = new ShellAPI.SHFILEOPSTRUCT();
fileop.wFunc = 3;
String[] paths = new String[files.length];
int ret;
for(ret = 0; ret < paths.length; ++ret) {
paths[ret] = files[ret].getAbsolutePath();
}
fileop.pFrom = new WString(fileop.encodePaths(paths));
fileop.fFlags = 1620;
ret = shell.SHFileOperation(fileop);
if (ret != 0) {
throw new IOException("Move to trash failed: " + fileop.pFrom + ": " + Kernel32Util.formatMessageFromLastErrorCode(ret));
} else if (fileop.fAnyOperationsAborted) {
throw new IOException("Move to trash aborted");
}
}
}
Код: Выделить всё
package com.jthink.songkong.analyse.duplicates;
import com.jthink.songkong.text.InfoMessage;
import com.jthink.songkong.ui.MainWindow;
import java.io.File;
import java.lang.reflect.Method;
import java.util.logging.Level;
public class WindowsDeleteTrash
{
public boolean deleteTrash( File file)
{
Boolean isHasTrash=false;
boolean result = false;
Object instance = null;
Class windowsFileUtilsClass = null;
try
{
windowsFileUtilsClass = Class.forName("com.sun.jna.platform.win32.W32FileUtils");
Method instanceClassMethod = windowsFileUtilsClass.getMethod("getInstance");
instance = instanceClassMethod.invoke(null);
Method instanceHasTrashMethod = windowsFileUtilsClass.getMethod("hasTrash");
isHasTrash = (Boolean) instanceHasTrashMethod.invoke(instance, null);
}
catch(Exception ex)
{
MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
return file.delete();
}
if (isHasTrash)
{
try
{
File[] files = new File[1];
files[0] = file;
Method instanceMoveMethod = windowsFileUtilsClass.getMethod( "moveToTrash", java.io.File[].class);
instanceMoveMethod.invoke(instance, files);
result = true;
}
//SONGKONG-1559
catch (java.lang.NoClassDefFoundError ncde)
{
MainWindow.logger.log(Level.SEVERE, InfoMessage.MSG_UNABLE_TO_MOVE_TO_TRASH.getMsg() + ncde.getMessage(), ncde);
result = file.delete();
}
//SONGKONG-1090
catch(Exception ex)
{
MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
result = file.delete();
}
}
else
{
result = file.delete();
}
return result;
}
}
Это трассировка стека:
Код: Выделить всё
java.lang.IllegalArgumentException: argument type mismatch
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:107)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at com.jthink.songkong.analyse.duplicates.WindowsDeleteTrash.deleteTrash(WindowsDeleteTrash.java:44)
Подробнее здесь: https://stackoverflow.com/questions/790 ... -match-the