{"id":"385dbbe6e004900d","repo":"junit-team/junit5","slug":"failed-to-create-a-java-net-uri-from-uri","errorCode":null,"errorMessage":"Failed to create a java.net.URI from: ${uri}","messagePattern":"Failed to create a java\\.net\\.URI from: (.+?)","errorType":"exception","errorClass":"PreconditionViolationException","httpStatus":null,"severity":"error","filePath":"junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java","lineNumber":86,"sourceCode":"\n\t/**\n\t * Create a {@code UriSelector} for the supplied URI.\n\t *\n\t * @param uri the URI to select; never {@code null} or blank\n\t * @see UriSelector\n\t * @see #selectUri(URI)\n\t * @see #selectFile(String)\n\t * @see #selectFile(File)\n\t * @see #selectDirectory(String)\n\t * @see #selectDirectory(File)\n\t */\n\tpublic static UriSelector selectUri(String uri) {\n\t\tPreconditions.notBlank(uri, \"URI must not be null or blank\");\n\t\ttry {\n\t\t\treturn new UriSelector(new URI(uri));\n\t\t}\n\t\tcatch (URISyntaxException ex) {\n\t\t\tthrow new PreconditionViolationException(\"Failed to create a java.net.URI from: \" + uri, ex);\n\t\t}\n\t}\n\n\t/**\n\t * Create a {@code UriSelector} for the supplied {@link URI}.\n\t *\n\t * @param uri the URI to select; never {@code null}\n\t * @see UriSelector\n\t * @see #selectUri(String)\n\t * @see #selectFile(String)\n\t * @see #selectFile(File)\n\t * @see #selectDirectory(String)\n\t * @see #selectDirectory(File)\n\t */\n\tpublic static UriSelector selectUri(URI uri) {\n\t\tPreconditions.notNull(uri, \"URI must not be null\");\n\t\treturn new UriSelector(uri);\n\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/junit-team/junit5/blob/956246301e03d757539f7b76dd5a91f298637563/junit-platform-engine/src/main/java/org/junit/platform/engine/discovery/DiscoverySelectors.java#L68-L104","documentation":"Thrown by DiscoverySelectors.selectUri(String) (line 80-88) when 'new URI(uri)' raises URISyntaxException. It is wrapped in a PreconditionViolationException. The blank-string guard runs first (Preconditions.notBlank), so this specifically means the string is non-blank but syntactically invalid per java.net.URI's strict RFC parsing.","triggerScenarios":"Calling DiscoverySelectors.selectUri(s) with an unencoded URI containing illegal characters (spaces, stray brackets, backslashes, or a missing scheme). Note: passing a java.net.URI object to selectUri(URI) bypasses this entirely.","commonSituations":"Passing a Windows file path (C:\\Users\\...) instead of a file:// URI; copy-pasting a URL with spaces or query characters not percent-encoded; mixing file: and raw paths; using a relative path where an absolute URI is expected.","solutions":["Pre-construct a URI with java.net.URI or Path.toUri() and pass it to selectUri(URI) to avoid re-parsing.","Percent-encode the string (URLEncoder / URI multi-arg constructor) before passing to selectUri(String).","For local files use DiscoverySelectors.selectFile(path) instead of a hand-built file:// URI.","Validate first: new URI(s) in a try/catch and log the exact URISyntaxException index/message."],"exampleFix":"// before\nvar selector = DiscoverySelectors.selectUri(\"C:\\\\Users\\\\me\\\\tests\\\\data.xml\"); // backslashes -> throws\n\n// after\nURI uri = Path.of(\"C:\\\\Users\\\\me\\\\tests\\\\data.xml\").toUri();\nvar selector = DiscoverySelectors.selectUri(uri);","handlingStrategy":"validation","validationCode":"URI uri;\ntry { uri = new URI(rawString); }\ncatch (URISyntaxException e) { throw new IllegalArgumentException(\"bad URI: \" + rawString, e); }\nvar selector = DiscoverySelectors.selectUri(uri); // URI overload avoids re-parse","typeGuard":"static boolean isParsableUri(String s) {\n    try { new URI(s); return true; } catch (URISyntaxException e) { return false; }\n}","tryCatchPattern":"try {\n    return DiscoverySelectors.selectUri(rawString);\n} catch (PreconditionViolationException e) {\n    throw new IllegalArgumentException(\"Not a valid URI: \" + rawString, e.getCause());\n}","preventionTips":["Pass a pre-built java.net.URI to selectUri(URI).","For files, use selectFile(path) instead of a file:// URI.","Percent-encode any user-derived string before treating it as a URI."],"tags":["junit-platform","discovery","uri","parsing","uri-selector"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}