0

I'm trying to run this code from my Linux terminal to check how the source code will run when it is put in package.

Here is the full path of the folder where the code is located: /home/mamun/Java/Basic and I am also running the code from this folder.

My Java version is: 11

Here is the code:

package io.demo;

import java.util.Set;
import java.util.HashMap;
import java.util.Map;
public class Hello
{
        public static void main(String[] args)
        {
                //create a map
                Map<String, String> map = new HashMap<>();

                //fill the map
                map.put("Hello", "World");
                map.put("Hi", "There");
                map.put("Welcome", "Here");


                //get the key set
                Set<String> keySet = map.keySet();

                //print the map
                for(String k : keySet)
                {
                        System.out.println(k  + " "+ map.get(k));       
                }
        }
}

I tied to solve my problem following this question, where many solutions has been suggested. Unfortunately so far I could find a solution and still trying to solve the problem.

The javac command could compile the source code to byte code as expected, but the the java io.demo.Hello command can't run the byte code. When I am running the byte code using java io.demo.Hello command, it's saying":

Error: Could not find or load main class Hello
Caused by: java.lang.NoClassDefFoundError: io/demo/Hello (wrong name: Hello)

When I am deleting the package statement, package io.demo; from the top, the code compile and run fine which means I have problem running the code when it locates inside a package. I also did try to run the code using this command java Hello but got same error.

Bye the way, should the Hello.class file be located in the following path, Basic/io/demo (Basic is the project folder, after compiling, compiler will create this path)? In my situation, the Hello.class file is located in the same folder where the Hello.java locates, which Basic

This code run fine in Eclipse.

Mamun
  • 375
  • 2
  • 8
  • 17
  • Is it `io.demo` or `id.demo` since you’ve used both several times – Sami Kuhmonen Jan 26 '19 at 17:18
  • Please update your question with showing the exact layout and location of files (and try to avoid confusion/typos like `id` vs `io`) and the command line you used and from what location you executed. Note that it looks like you were in that `Basic/io/demo` folder and tried `java Hello`, while you should be in the `Basic` folder and execute `java io.demo.Hello` instead. – Mark Rotteveel Jan 26 '19 at 17:19
  • The accepted answer to the duplicate question addresses and explains most (if not all) permutations of this problem. Yours seems to be a combination of Reason 1 - example 1 and Reason 2b. – Mark Rotteveel Jan 26 '19 at 17:23
  • @SamiKuhmonen, `id.demo` was type, it should be `io.demo`. – Mamun Jan 26 '19 at 17:48
  • @MarkRotteveel, I am sorry for my poor presentation of the question. I have updated the question and tried to make it more clearer. I am in folder `Basic`, what I mean by `Basic/io/demo` is that I want to know _whether the compiler should make sub folders in the main folder according to the package declaration, `Basic` and put the source code in newly created sub folders(`/io/demo`)_ and also running the code from `Basic` folder. – Mamun Jan 26 '19 at 17:54
  • Your class file still has **id** in its package. The directories must match the parts of the package name. If the package is `id.demo`, then directories must be `id/demo` (or the other way around, if it is in `io/demo`, then the package must be `io.demo`). – Mark Rotteveel Jan 27 '19 at 09:16
  • @MarkRotteveel, corrected the typo, **id** to **io**. Well, no matter what's the package name or hierarchy, whether it's `demo` or `io.demo`, the out put result is always same error. I have tried to solve the problem following the question you have added, unfortunately so far could not solve. I am still continuing to follow that question. – Mamun Jan 27 '19 at 10:41

0 Answers0