-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run GroovyModuleLoaderExample in IDEA , Win7 Path error #40
Comments
I get the same exception. |
We need to replace import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
public class WindowsPathIssue {
public static void main(String[] args) {
Path pathToNotepad = Paths.get("/C:/Windows/notepad.exe");
System.out.println(FileUtils.sizeOf(pathToNotepad.toFile()));
}
} |
Here is a test case for @Test
public void testGetJarPathFromUrl() throws Exception {
URL posixPath = new URL("jar:file:/path/to/groovy.jar!/path/to/resource.txt");
URL windowsPath = new URL("jar:file:/C:/path/to/groovy.jar!/path/to/resource.txt");
assertEquals(ClassPathUtils.getJarPathFromUrl(posixPath).toString(), "/path/to/groovy.jar");
assertEquals(ClassPathUtils.getJarPathFromUrl(windowsPath).toString(), "C:/path/to/groovy.jar");
} |
Hmm, seems that we can't influence how System.out.println(Paths.get("/foo/bar")); // outputs "\foo\bar". |
This code should solve the issue: @Test
public void testGetJarPathFromUrl() throws Exception {
// Regression tests for https://github.com/Netflix/Nicobar/issues/40.
URL pathOnPosix = new URL("jar:file:/path/to/groovy.jar!/path/to/resource.txt");
URL pathOnWindows = new URL("jar:file:/C:/path/to/groovy.jar!/path/to/resource.txt");
URL pathWithSpaces = new URL("jar:file:/D:/path to/groovy.jar!/path to/resource.txt");
assertEquals("/path/to/groovy.jar",
ClassPathUtils.getJarPathFromUrl(pathOnPosix).toString().replace("\\", "/"));
assertEquals("C:/path/to/groovy.jar",
ClassPathUtils.getJarPathFromUrl(pathOnWindows).toString().replace("\\", "/"));
assertEquals("D:/path to/groovy.jar",
ClassPathUtils.getJarPathFromUrl(pathWithSpaces).toString().replace("\\", "/"));
} Version 1: public static Path getJarPathFromUrl(URL jarUrl) {
String jarPath = jarUrl.getPath();
int startIndex = jarPath.startsWith("file:") ? 5 : 0;
int endIndex = jarPath.lastIndexOf("!");
String jarFilePath = jarPath.substring(startIndex, endIndex);
return Paths.get(new File(jarFilePath).getPath());
} Version 2: public static Path getJarPathFromUrl(URL jarUrl) {
try {
JarURLConnection connection = (JarURLConnection) jarUrl.openConnection();
URL jarFileUrl = connection.getJarFileURL();
String jarFilePath = new File(jarFileUrl.toURI()).getPath();
return Paths.get(jarFilePath);
}
catch (Exception exception) {
throw new RuntimeException(exception);
}
} |
There is another exception with
|
The class public String toString() {
return name + ":" + slot;
} |
My suggestion is, to add a wrapper method for public static Path getPath(String pathString) {
File pathFile = new File(pathString);
return Paths.get(pathFile.getParent(), pathFile.getName().replace(":", "_"));
} With this approach I finally managed to get the
|
@hastebrot did you run into problem with the example if you update jar file at runtime? |
@whisperwing I haven't tested this, yet. |
@hastebrot I found that replacing jar file without name change wouldn't work. Likely because of #37 |
Fixing Windows compatibility #40
Fixing Windows compatibility Netflix#40
The text was updated successfully, but these errors were encountered: