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.