Skip to content

Instantly share code, notes, and snippets.

@nimatrueway
Last active September 21, 2020 09:21
Show Gist options
  • Save nimatrueway/baecf702d04f2b2f26385a6e4da65212 to your computer and use it in GitHub Desktop.
Save nimatrueway/baecf702d04f2b2f26385a6e4da65212 to your computer and use it in GitHub Desktop.
Java classpath/classloaders binary conflict test
#!/bin/sh
rm -rf dir1 dir2;
mkdir -p dir1/pkg;
cat >File.java <<EOL
package pkg;
public class File {
public static void main(String[] args) {
System.out.println("dir1/File");
}
}
EOL
javac File.java;
mv File.class ./dir1/pkg;
mkdir -p dir2/pkg;
cat >File.java <<EOL
package pkg;
public class File {
public static void main(String[] args) {
System.out.println("dir2/File");
}
}
EOL
javac File.java;
mv File.class ./dir2/pkg;
OUTPUT_DIR1_DIR2=$(java -cp dir1:dir2 pkg.File)
OUTPUT_DIR2_DIR1=$(java -cp dir2:dir1 pkg.File)
echo ""
echo " RESULT OF 'java -cp dir1:dir2 pkg.File' =>"
echo " $OUTPUT_DIR1_DIR2"
echo ""
echo " RESULT OF 'java -cp dir2:dir1 pkg.File' =>"
echo " $OUTPUT_DIR2_DIR1"
echo ""
rm File.java;
rm -rf dir1 dir2;
@nimatrueway
Copy link
Author

nimatrueway commented Mar 10, 2019

Trying to prove the point mentioned by AlexR in:
https://stackoverflow.com/a/9757698/1556045

default URLClassLoader picks up class-file content from the first classpath entry

Read more in depth about classloaders in:
https://www.cs.rit.edu/~ark/lectures/cl/05_02_00.html

@nimatrueway
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment