0

There is a class a.b.c.Hello of which I would just get the Hello.class file delivered. I would like to run using something like:

java Hello //or java a.b.c.Hello 

This leads me to a NoClassDefFoundError. Normally I'd have the a/b/c dirs with the class insdie and I'd add them to the -classpath option, but:

  • Is there a way of running a class in a package like this without having to put it inside a/b/c to match the package name?

All solutions I've found state the directory structure has to match the package naming, but I'd like to run the .class file directly from the folder where it is without recreating the folder structure

2 Answers 2

1

Is there a way of running a class in a package like this without having to put it inside a/b/c to match the package name?

Yes: By using a jar. Put the .class file in the jar (with the correct path), and then:

java -cp TheJarFile.jar a.b.c.Hello 

This is, of course, very much like putting it in an a/b/c directory; it's just that the directory is in the jar, not the file system.

Sign up to request clarification or add additional context in comments.

Comments

0

The packages do not just structure your classes (in folders), they also create a namespace. While the simple class name is Hello, the real class name is a.b.c.Hello.

That's because class names might repeat frequently (in different libs, f.e), but must be addressable on the other hand. For example: 'User', 'Logger', 'List', 'Date'.

Its not recommended, but you can put your class in the default package, too. Just remove the 'package ...' line.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.