Java uncatchable ClassNotFoundException when using java.awt.Clipboard

92 Views Asked by At

I get the following exception when running this code

private static String getClipboard() {
        try {

            return (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);

        } catch (IOException | UnsupportedFlavorException e) {
            System.exit(-1);
            return "";
        }
    }
Exception "java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData"while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsight.editorActions.FoldingData
Exception "java.lang.ClassNotFoundException: com/intellij/codeInsight/editorActions/FoldingData"while constructing DataFlavor for: application/x-java-jvm-local-objectref; class=com.intellij.codeInsight.editorActions.FoldingData
Exception "java.lang.ClassNotFoundException: com/intellij/openapi/editor/impl/EditorCopyPasteHelperImpl$CopyPasteOptionsTransferableData"while constructing DataFlavor for: application/x-java-serialized-object; class=com.intellij.openapi.editor.impl.EditorCopyPasteHelperImpl$CopyPasteOptionsTransferableData
Exception "java.lang.ClassNotFoundException: com/intellij/openapi/editor/impl/EditorCopyPasteHelperImpl$CopyPasteOptionsTransferableData"while constructing DataFlavor for: application/x-java-serialized-object; class=com.intellij.openapi.editor.impl.EditorCopyPasteHelperImpl$CopyPasteOptionsTransferableData

I haven't found a clear solution to completely solve this. Someone suggested to print every error in a file, but this wouldn't solve the problem and would "hide" it instead. Any idea on how to prevent that exception from happening?

1

There are 1 best solutions below

0
DuncG On

You should always check what the available clipboard formats are before accessing, as it may not always contain a String. You can do this with:

if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor))
...

This will print what formats are available:

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

System.out.println("hasString "+clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor));
for(var f : clipboard.getAvailableDataFlavors())
{
    System.out.println(f);
}

It is never a good plan to slip System.exit() into any handler - remove. In theory the clipboard could be changed by another app between checking so you need your exception handler to return "" or throw it as your own exception type.

static String getClipboard() {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    try {
        // Note that it is still possible for another app to modify clipboard
        // between "if" and "return", so this could still fail:
        if(clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
            return (String) clipboard.getData(DataFlavor.stringFlavor);
        }
    } catch (Exception e) {
        // or rethrow as your own exception type
    }
    return "";
}