Error: Could Non Notice Or Charge Principal Degree Inwards Coffee [Solved]

Error: Could non honour or charge primary shape HelloWorld comes when y'all are trying to run your Java programme using coffee command amongst the primary shape equally HelloWorld but Java is non able to honour the class. In lodge to solve this error, y'all must know how Java honour as well as loads the classes, that's a picayune fleck complex topic for beginners, but nosotros volition touching on the same base of operations here. For the curious reader, I would propose reading my post service How Classpath plant inwards Java, a must read for a beginner. For now, y'all merely think that at that spot is an surroundings variable called CLASSPATH which includes directories where Java looks for all shape files as well as if it doesn't honour your primary shape at that spot as well as thence it throws "Error: Could non honour or charge primary shape XXX", where XXX is the elevate of your primary class.

Since many Java programmer is instantly started programming using Eclipse they human face upwards this final result when they starting fourth dimension attempt to run their Java programme from command line. In Eclipse, it's tardily to compile as well as run the programme because Eclipse takes aid of all Classpath setup, but when y'all run your Java programme from command line, CLASSPATH surroundings variable comes inwards picture.

Personally, I don't similar this surroundings variable as well as doesn't define inwards my surroundings variable, because its confusing as well as rootage of thence many classpath related issue. Instead, I utilization -cp or -classpath selection amongst java command to run my program. This means y'all ever know which JARs are included inwards your classpath.


For beginners, roughly other of import thing to empathize is the difference betwixt PATH as well as CLASSPATH, y'all must know that PATH is used locate organisation executable, commands or .exe, .dll files (in Windows) as well as .so files (in Linux). It is equally good used to locate native libraries used past times your Java program. While, CLASSPATH is used to locate the shape file or JAR files. It's Java shape loader who looked into CLASSPATH for loading classes.


Coming dorsum to the employment inwards hand, if y'all are a beginner inwards Java, who are able to run the programme from Eclipse but getting "Error: Could non honour or charge primary shape HelloWorld" when trying to run the same programme from the command business as well as thence follow the steps given hither to solve it.



Solving Error: Could non honour or charge primary shape HelloWorld 

Unfortunately beginner's majority similar HelloWorld program for our testing, interestingly I lead maintain named it HelloHP as well as it resides inwards a parcel called "dto". I lead maintain purposefully chosen a shape amongst a parcel instead of HelloWorld inwards the default parcel because many programmers acquire "Could non honour or charge primary class" mistake when they attempt to run a shape which is within a package.

package dto; /**  * Simple Java programme to demonstrate next mistake  * Error :Could non honour or charge primary shape  *   * @author Javin Paul  */ public class HelloHP {      public static void main(String args[]) {         System.out.println("My starting fourth dimension programme inwards Java, HelloWorld !!");             }  }

When y'all run this from Eclipse, past times Right click on the rootage file as well as Choosing "Run equally Java Program", it volition run fine as well as impress next line:

My starting fourth dimension programme inwards Java, HelloWorld !!

Everything equally expected, Now nosotros volition attempt to run same Java programme from command line. Since I am using Maven amongst Eclipse, its create procedure creates shape files inwards project_directory\target\classes directory. If y'all are non using Maven amongst Eclipse, as well as thence y'all tin sack run across the shape file created past times Eclipse's Java compiler inwards project_directory\bin. It doesn't affair how those shape files are created, but, what is of import is the place of the shape file.

If your shape is within a non-default package e.g. "dto" inwards our illustration as well as thence compiler the volition lay the HelloHP.class file, which contains Java bytecode inwards a directory named "dto". In our illustration the total elevate of shape dto.HelloHP as well as it is acquaint inwards C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto. So inwards the starting fourth dimension try, I acquire at that spot as well as execute coffee command to launch my program, equally seen below:

C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>java HelloHP Error: Could non honour or charge primary class HelloHP

Do y'all run across the error? It's coming because the total elevate of the shape should hold upwards dto.HelloHP as well as non HelloHP. So let's right this mistake as well as attempt to run the same command from the same place but this fourth dimension amongst fully qualified name:

C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>java dto.HelloHP Error: Could non honour or charge primary class dto.HelloHP

Still same error, right. Why? because I don't lead maintain whatever CLASSPATH surroundings variable, neither I am using -classpath or -cp selection to propose the path, So by default Java is alone searching inwards the electrical flow directory. It is looking for dto/HelloHP.class but since nosotros are already within dto, it is  not able to honour the class. So, what should nosotros attain now? let's acquire to the rear directory "C:\Users\WINDOWS 8\workspace\Demo\target\classes" as well as execute the same command, this time, it should work:

C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto>cd ..  C:\Users\WINDOWS 8\workspace\Demo\target\classes>java dto.HelloHP My starting fourth dimension programme inwards Java, HelloWorld !!

Bingo!!, our programme ran successfully because, without whatever hint close where to honour shape files, Java is past times default looking into the electrical flow directory, denoted past times . (dot) as well as able to locate ./dto/HelloHP.class.

Now, what if y'all desire to run this programme from whatever other directory? Well, for that utilization whether nosotros postulate to define CLASSPATH or merely utilization -classpath or -cp option. I similar the instant selection because it's easier to command as well as change. Also, remember, it overrides whatever CLASSPATH surroundings variable. If y'all similar to set CLASSPATH surroundings variable inwards Windows, run across that tutorial.

Now let's run the programme target directory starting fourth dimension without using -classpath option:

C:\Users\WINDOWS 8\workspace\Demo\target\classes>cd ..  C:\Users\WINDOWS 8\workspace\Demo\target>java dto.HelloHP Error: Could non honour or charge primary class dto.HelloHP

You tin sack run across nosotros are 1 time again started getting the same error, Why? because Java is yet looking into the electrical flow directory as well as at that spot is no .\target\dto\HelloHP.class there, equally it's 1 score downward e.g. .\target\classes\dto\HelloHP.class

Now let's run the same command using -classpath selection from target directory itself:

C:\Users\WINDOWS 8\workspace\Demo\target>java -cp ./classes;. dto.HelloHP My starting fourth dimension programme inwards Java, HelloWorld !!

Bingo!!, our programme ran successfully 1 time again because instantly Java is equally good looking at ./classes directory as well as at that spot it is able to honour dto\HelloHP.class file.

There are many ways Error: Could non honour or charge primary shape HelloWorld manifests itself, but if y'all know the basics of Java Classpath, y'all can easily variety out the problem. Most of the fourth dimension y'all merely postulate to either right your CLASSPATH surroundings variable or run your programme amongst java -cp or -classpath option. By the way, at that spot are to a greater extent than to it e.g. Main shape defined inwards the manifest.mf file as well as that's why I propose reading close How Classpath plant inwards Java (see the link inwards the starting fourth dimension paragraph).

Summary

If y'all are getting "Error: Could non honour or charge primary shape XXX", where XXX is the elevate of your primary shape piece running Java programme as well as thence attain this to solve that error:

1) If y'all are running Java programme right from the directory where .class file is as well as y'all lead maintain CLASSPATH surroundings variable defined as well as thence brand certain it include electrical flow directory. (dot). You tin sack include it equally set CLASSPATH=%CLASSPATH%;. inwards Windows as well as export CLASSPATH = ${CLASSPATH}:. (see the separator, inwards Windows it's;(semicolon) piece inwards Linux it is (colon), equally good banknote nosotros lead maintain included electrical flow directory inwards existing classpath. If y'all yet human face upwards the final result of setting classpath, run across this step past times measurement guide to laid the classpath. Same thing applies if y'all are running your programme using -cp or -classpath option.


2) If y'all are running Java programme from the directory, your .class file is as well as y'all don't lead maintain whatever CLASSPATH or -cp selection as well as thence banking corporation tally whether your shape is the inwards the parcel or not. If it's the inwards the parcel as well as thence acquire exterior of the parcel directory as well as run coffee command amongst fully qualified elevate e.g. if your programme is com.abc parcel as well as thence runs next command from the rear directory of "com"

java com.abc.HelloWorld

without whatever classpath hints, Java volition facial expression into the electrical flow directory as well as search for com\abc\HelloWorld.class inwards Windows, thence if com directory exists inwards your electrical flow directory, your programme volition run otherwise y'all volition acquire "Error: Could non honour or charge primary shape dto.HelloHP".


3) You tin sack run your Java programme from anywhere amongst the aid of proper CLASSPATH or coffee -cp selection equally shown below:

java -cp C:\test\;. com.abc.HelloWorld


If y'all yet facing whatever final result merely banking corporation tally whether y'all lead maintain accidentally using CLASSPATH surroundings variable, y'all tin sack banking corporation tally this inwards Windows past times running echo %CLASSPATH% command as well as inwards Linux past times running echo $CLASSPATH. If CLASSPATH is nonempty as well as thence it volition impress its value otherwise merely echo the same command.

4) If y'all are running inwards Java version 1.6 or 1.5, as well as thence instead of receiving "Error: Could non honour or charge primary class", y'all volition get Exception inwards thread "main" java.lang.NoClassDefFoundError: HelloWorld. It's alone from JDK 1.7 onward nosotros are started receiving this novel error. The solution is just same, every fleck of news applies to that illustration equally well. So if y'all are non able to solve that employment past times next steps here, attain allow me know as well as I volition attempt to travel amongst y'all to troubleshoot the problem.

Here is the screenshot of how I tried to reproduce as well as solve the mistake equally discussed inwards the previous paragraph:

 Could non honour or charge primary shape HelloWorld Error: Could non honour or charge primary shape inwards Java [Solved]



That's all close how to solve "Error: Could non honour or charge primary shape HelloWorld" inwards Java. Classpath is picayune confusing topic to master, but y'all volition empathize it 1 time y'all started writing as well as running roughly Java program. If y'all are yet non able to create your employment as well as thence post service a comment at that spot amongst what y'all lead maintain tried as well as nosotros volition attempt to troubleshoot together. My finish is non merely to give y'all solution but equally good brand y'all able to explicate why the solution is working as well as CLASSPATH basics are rattling of import for a Java developer. I lead maintain seen many programmers getting frustrated, losing involvement inwards Java due to diverse PATH as well as CLASSPATH issues e.g. NoClassDefFoundError as well as ClassNotFoundException as well as this is my humble endeavor to pick out them dorsum as well as empower amongst practical knowledge. Hope y'all understand.


Further Reading
Building debugging as well as troubleshooting science is non tardily as well as it takes lots of do as well as sense to understand, reproduce as well as solve the error. If y'all are novel Java developer as well as thence y'all starting fourth dimension pick a majority to acquire Java from start to end, but if y'all are intermediate Java developer as well as thence y'all should facial expression at the next resources to prepare your debugging skill.

Understanding the Java Virtual Machine: Class Loading as well as Reflection
The Complete Java MasterClass
Core Java for the Impatient - Covers Java SE 8

Komentar

Postingan populer dari blog ini

Difference Betwixt Struts Validatorform Vs Validatoractionform - Interview Question

How To Convert Inputstream To Byte Array Inwards Coffee - Two Examples

Difference Betwixt Fileinputstream Together With Filereader Inwards Coffee | Inputstream Vs Reader