1

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?

3
  • 1
    but this wouldn't solve the problem and would "hide" it instead. You couldn't hide it more than you are doing at the moment (swallowing the exception and terminating your app). I suppose you could argue that not terminating would be hiding it more. What you should be doing is logging exceptions, or a least printing the stack trace Commented Feb 29, 2024 at 14:01
  • 1
    What makes this "uncatchable"? Are you trying to deal with the exception? Or are you asking to improve your try block since you are asking 'how to prevent that exception from happening'? Commented Feb 29, 2024 at 14:54
  • Edit your question and show the complete stack trace for at least one of those exceptions. The code you have shown us does not construct any DataFlavors, much less an application/x-java-jvm-local-objectref or application/x-java-serialized-object DataFlavor. Commented Feb 29, 2024 at 23:23

2 Answers 2

2

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 "";
}
Sign up to request clarification or add additional context in comments.

Comments

1

The solution with isDataFlavorAvailable did not work for me, because the exceptions are fired during loading list of available flavors. Fixed by ignoring all those "intellij idea" formats by:

SystemFlavorMap flavorMap = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
flavorMap.setFlavorsForNative(
    "JAVA_DATAFLAVOR:application/x-java-jvm-local-objectref; class=com.intellij.codeInsight.editorActions.FoldingData",
    new DataFlavor[]{DataFlavor.getTextPlainUnicodeFlavor()});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.