3
package demo;
import java.io.*;
class A
{
    public void run()
    {
        System.out.println("This Is Class A : public void run()");
    }
}
class B
{
    public static void main(String args[])
    {
        System.out.println("Main Method Executed");
        A obj1 = new A();
        obj1.run();
    }
}

Compile :

d:\java>javac -d . demo.java

---> class file be created in directory demo [ A.class, B.class]

Run : d:\java>java B
Error: could not find or load main class B

but if I remove the line 1 [package demo;] than it run proper. so, when we use package name than why "Error: could not find or load main class B" error be generated.

1
  • 2
    Try to start with : java demo.A Commented Sep 27, 2015 at 11:46

4 Answers 4

2

Run this command. Because main method is in B class. The name of the package is demo and class containing main method is B.

java demo.B

Output :
Main Method Executed
This Is Class A : public void run()

but if I remove the line 1 [package demo;] than it run proper.

This because when you provide the package declaration in your program, then your classes reside in the package. So you need to provide the complete path to access them from your package.

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

6 Comments

thanks, I done it. but if I go on package directory like
but if I go on package directory like > cd demo [set current directory 'd:\java\demo>'] than I try >java B, than also give "error could not find or load main class B" while B.class reside in demo[package] directory.
@KumarHarsha I never said to inside the directory. Just run the command after compiling your code. Original which you have posted here.
@KumarHarsha the answer you have accepted have asked you change you couple of things. don't change anything into your original code. compile with the command you have pasted and then run with my command from the answer.
yes your answer is correct ... that way I done this. but I face some problem when I go on package directory than try to run >java B. but it has same error, please help. how it working.
|
2

it should be as below since you have declared a package structure and the class file should reside in such a folder structure. As in your A.class should be in demo folder

>java demo/A

Also it seems you have main method in class B. So you'll have to execute class B instead of A

>java demo/B

Comments

0

You should rename your demo.java as B.java and use

public class B

instead of

class B

Suppose B.class is located at C:\

To compile B.java, type

C:\javac -d . B.java

This would create a folder named "demo" which contains 2 class files named A.class and B.class.

To execute the program, type

C:\demo\java demo.B

Here's the result of the program:

Main Method Executed
This Is Class A : public void run()

Comments

0

Suppose your Main class resides in the controller package. if you're using Gradle for building and testing your application, then make sure you've these lines in your build.gradle file:

jar {
    manifest {
      def classpath = configurations.runtime.collect { jarfile ->
        '../lib/' + jarfile.getName()
      }.join(' ')

       attributes 'Main-Class': 'controller.Main', 'Class-Path': classpath, 'Implementation-Version': version
    }
}

task runit(type: JavaExec) {
    main 'controller.Main'
    classpath = sourceSets.main.runtimeClasspath
}

Note: controller is the name of my package. My Main class is inside the controller package, and it works well, even executing as a standalone application. Remember to change the name of the package of where your Main class resides.

Comments

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.