0

I am newbie in java. I am running java programs via Ubuntu terminal I just started java package topic and have been dealing with a problem for several hours. I tried to create a simple package called pack which has single class Hello. I created a directory pack. Inside the pack I put Hello.class file intp pack directory via

javac -d ./pack Hello.java

command. Then I included pack package into a class containing main method. The class's name is test and it is located in test.java file This class is located in another directory. I compile via

javac -cp ./pack test.java

It compiles without any error and everything is ok. However, when I enter command

java -cp ./pack test

it gives me

Error: Could not find or load main class test

When I tried

java test

command. The following message showed up

Exception in thread "main" java.lang.NoClassDefFoundError: pack/Hello
at test.main(test.java:6)
Caused by: java.lang.ClassNotFoundException: pack.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more

Can anybody explain me what I am doing wrong? Any help is much appreciated.

Sorry, I did bot include my source codes. Here they are.

import pack.Hello;
public class test
{
    public static void main(String args[])
    {
        Hello.HelloMessage();
    }
}

This is test.java file which tests if everything is ok. It is located at

/home/uesername/apps

directory.

Then I created "pack" directory. Full path to the pack directory is

home/username/apps/pack

Inside the "pack" I put Hello.java file. The content of Hello.java file is

package pack;
public class Hello
{
    public static void HelloMessage()
    {
        System.out.println("hello, world");
    }
}
2
  • " I created a directory pack. Inside the pack I put Hello.class file intp pack ". -d flag creates the package structure for you don't do it yourself Commented Mar 23, 2016 at 9:05
  • Can you show the source code. We need at least (from test.java) the line that starts with "package" and the line that "import"s Hello. And from Hello.java we need the line that starts with "package". Commented Mar 23, 2016 at 9:09

1 Answer 1

2

To begin with I suggest you use an IDE to setup you environment for compiling, running and debugging.

The problem you have is that you have compiled with the wrong path.

javac -cp . pack/Hello.java
javac -cp . pack/test.java

and

java -cp . pack.Hello

or

java -cp . pack.test

The problem is that you compiled a class with package pack in a directory pack and you would end up with

pack/pack/Hello.class

I suggest you check where the Hello.class file has been placed.

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

2 Comments

You are right. Hello.class file is in pack/pack directory. I copied it into pack directory. But when I enter java command it still gives me error messages
@olzhabala which is why I suggest using an IDE which will setup the directories for you, compile and run the code and make it easier to debug it too. I use IntelliJ Community Edition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.