Chuck Worker
Chuck Worker

Reputation: 267

Using Java application launcher

I have class Hello. I have successfully compiled the .class file from it and placed it into dir/subdir directory and have assigned it the dir.subdir package in its code. And I want to run it from the command line with java command.

I have run it with command: java dir/subdir/Hello and it ran successfully! But I read in docs that it should be done with simply fully-qualified class name. I tried to execute: java dir.subdir.Hello and it ran successfully too!!

Which of these ways is sound approach and more correctly? What does each of them specifically means? What is their fundamental difference?

Upvotes: 8

Views: 650

Answers (4)

Frostsoft
Frostsoft

Reputation: 46

Definitely use dots for it is then able for cross platform running. Mac/WIndows/Linux

Upvotes: 0

Akash
Akash

Reputation: 1

You should always use dots as this works for all OS (win/unix/mac). Package is nothing but directory Structure which in windows is treated with a slash (/) , so java is able to run your program without any error.

Upvotes: 0

Brian
Brian

Reputation: 17309

You should use the dotted form, but not because of platform compatibility.

The argument dir/subdir/Hello is valid to use here because Java's default ClassLoader implementation processes it correctly. However, not all ClassLoader implementations support this. You should use the dotted form because according to the documentation in ClassLoader.loadClass names are supposed to be binary names. The JLS defines binary names in JLS 13.1, item 1:

The class or interface must be named by its binary name, which must meet the following constraints:

  • The binary name of a top level type is its canonical name.

  • The binary name of a member type consists of the binary name of its immediately enclosing type, followed by $, followed by the simple name of the member.

  • The binary name of a local class consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits, followed by the simple name of the local class.

  • The binary name of an anonymous class consists of the binary name of its immediately enclosing type, followed by $, followed by a non-empty sequence of digits.

  • The binary name of a type variable declared by a generic class or interface is the binary name of its immediately enclosing type, followed by $, followed by the simple name of the type variable.

  • The binary name of a type variable declared by a generic method is the binary name of the type declaring the method, followed by $, followed by the descriptor of the method as defined in The Java Virtual Machine Specification, Java SE 7 Edition, followed by $, followed by the simple name of the type variable.

  • The binary name of a type variable declared by a generic constructor is the binary name of the type declaring the constructor, followed by $, followed by the descriptor of the constructor as defined in The Java Virtual Machine Specification, Java SE 7 Edition, followed by $, followed by the simple name of the type variable.

Upvotes: 1

Using the package name (with dots) is better, since it is cross-platform (remember that Windows uses \) and is closer to what you would need if your program is in a jar file (where Main-Class has to be specified as a class name).

Upvotes: 0

Related Questions