Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "." and "/" in java classname?

Tags:

java

class

I'm new to java. When I try to learn Maven in 5 minutes, I found that this command

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

worked the same way as

java -cp target/my-app-1.0-SNAPSHOT.jar com/mycompany/app/App

It drives me crazy because the last argument in the second command is actually a path. What is the difference between "." and "/" in java classname?

I have looked up some articles but still don't get it.

like image 976
爱喝芒果汁 Avatar asked May 14 '26 11:05

爱喝芒果汁


1 Answers

This is an implementation detail leaking out. Largely for irrelevant historical reasons, class names in the language are dot-separated but class names in the classfile format are slash-separated. (https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.2.) For the most part, internal names are not visible to users, but they do leak in some circumstances. Many tools that deal with classfiles will convert from the external (dotted) to internal (slashed) name using something like replace('.', '/'), which has the effect that internal names are also accepted by the tool. That's what's going on here.

like image 57
Brian Goetz Avatar answered May 16 '26 01:05

Brian Goetz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!