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

Error: Could non respect or charge top dog shape HelloWorld comes when you lot are trying to run your Java programme using coffee command amongst the top dog shape every bit HelloWorld but Java is non able to respect the class. In fellowship to solve this error, you lot must know how Java respect in addition to loads the classes, that's a piffling fleck complex theme for beginners, but nosotros volition acquit upon the same base of operations here. For the curious reader, I would advise reading my post How Classpath industrial plant inwards Java, a must read for a beginner. For now, you lot only retrieve that in that place is an surroundings variable called CLASSPATH which includes directories where Java looks for all shape files in addition to if it doesn't respect your top dog shape in that place in addition to then it throws "Error: Could non respect or charge top dog shape XXX", where XXX is the cite of your top dog class.

Since many Java programmer is straight off started programming using Eclipse they confront this final result when they kickoff seek to run their Java programme from command line. In Eclipse, it's slowly to compile in addition to run the programme because Eclipse takes attention of all Classpath setup, but when you lot run your Java programme from command line, CLASSPATH surroundings variable comes inwards picture.

Personally, I don't similar this surroundings variable in addition to doesn't define inwards my surroundings variable, because its confusing in addition to beginning of hence many classpath related issue. Instead, I utilisation -cp or -classpath selection amongst java command to run my program. This means you lot ever know which JARs are included inwards your classpath.


For beginners, to a greater extent than or less other of import thing to empathise is the difference betwixt PATH in addition to CLASSPATH, you lot must know that PATH is used locate arrangement executable, commands or .exe, .dll files (in Windows) in addition to .so files (in Linux). It is too 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 occupation inwards hand, if you lot are a beginner inwards Java, who are able to run the programme from Eclipse but getting "Error: Could non respect or charge top dog shape HelloWorld" when trying to run the same programme from the command business in addition to then follow the steps given hither to solve it.



Solving Error: Could non respect or charge top dog shape HelloWorld 

Unfortunately beginner's mass similar HelloWorld program for our testing, interestingly I receive got named it HelloHP in addition to it resides inwards a bundle called "dto". I receive got purposefully chosen a shape amongst a bundle instead of HelloWorld inwards the default bundle because many programmers acquire "Could non respect or charge top dog class" fault when they seek to run a shape which is within a package.

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

When you lot run this from Eclipse, past times Right click on the beginning file in addition to Choosing "Run every bit Java Program", it volition run fine in addition to impress next line:

My kickoff programme inwards Java, HelloWorld !!

Everything every bit expected, Now nosotros volition seek 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 you lot are non using Maven amongst Eclipse, in addition to then you lot tin give the 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 instance in addition to then compiler the volition set the HelloHP.class file, which contains Java bytecode inwards a directory named "dto". In our instance the total cite of shape dto.HelloHP in addition to it is acquaint inwards C:\Users\WINDOWS 8\workspace\Demo\target\classes\dto. So inwards the kickoff try, I acquire in that place in addition to execute coffee command to launch my program, every bit seen below:

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

Do you lot run across the error? It's coming because the total cite of the shape should move dto.HelloHP in addition to non HelloHP. So let's right this fault in addition to seek 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 respect or charge top dog class dto.HelloHP

Still same error, right. Why? because I don't receive got whatever CLASSPATH surroundings variable, neither I am using -classpath or -cp selection to advise the path, So by default Java is exclusively searching inwards the electrical current directory. It is looking for dto/HelloHP.class but since nosotros are already within dto, it is  not able to respect the class. So, what should nosotros reach now? let's acquire to the raise directory "C:\Users\WINDOWS 8\workspace\Demo\target\classes" in addition to 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 kickoff programme inwards Java, HelloWorld !!

Bingo!!, our programme ran successfully because, without whatever hint almost where to respect shape files, Java is past times default looking into the electrical current directory, denoted past times . (dot) in addition to able to locate ./dto/HelloHP.class.

Now, what if you lot desire to run this programme from whatever other directory? Well, for that role whether nosotros postulate to define CLASSPATH or only utilisation -classpath or -cp option. I similar the minute selection because it's easier to command in addition to change. Also, remember, it overrides whatever CLASSPATH surroundings variable. If you lot similar to set CLASSPATH surroundings variable inwards Windows, run across that tutorial.

Now let's run the programme target directory kickoff 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 respect or charge top dog class dto.HelloHP

You tin give the sack run across nosotros are in i lawsuit again started getting the same error, Why? because Java is even hence looking into the electrical current directory in addition to in that place is no .\target\dto\HelloHP.class there, every bit it's i 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 kickoff programme inwards Java, HelloWorld !!

Bingo!!, our programme ran successfully in i lawsuit again because straight off Java is too looking at ./classes directory in addition to in that place it is able to respect dto\HelloHP.class file.

There are many ways Error: Could non respect or charge top dog shape HelloWorld manifests itself, but if you lot know the basics of Java Classpath, you lot can easily form out the problem. Most of the fourth dimension you lot only postulate to either right your CLASSPATH surroundings variable or run your programme amongst java -cp or -classpath option. By the way, in that place are to a greater extent than to it e.g. Main shape defined inwards the manifest.mf file in addition to that's why I advise reading almost How Classpath industrial plant inwards Java (see the link inwards the kickoff paragraph).

Summary

If you lot are getting "Error: Could non respect or charge top dog shape XXX", where XXX is the cite of your top dog shape spell running Java programme in addition to then reach this to solve that error:

1) If you lot are running Java programme right from the directory where .class file is in addition to you lot receive got CLASSPATH surroundings variable defined in addition to then brand certain it include electrical current directory. (dot). You tin give the sack include it every bit set CLASSPATH=%CLASSPATH%;. inwards Windows in addition to export CLASSPATH = ${CLASSPATH}:. (see the separator, inwards Windows it's;(semicolon) spell inwards Linux it is (colon), too regime notation nosotros receive got included electrical current directory inwards existing classpath. If you lot even hence confront the final result of setting classpath, run across this step past times mensuration guide to laid the classpath. Same thing applies if you lot are running your programme using -cp or -classpath option.


2) If you lot are running Java programme from the directory, your .class file is in addition to you lot don't receive got whatever CLASSPATH or -cp selection in addition to then cheque whether your shape is the inwards the bundle or not. If it's the inwards the bundle in addition to then acquire exterior of the bundle directory in addition to run coffee command amongst fully qualified cite e.g. if your programme is com.abc bundle in addition to then runs next command from the raise directory of "com"

java com.abc.HelloWorld

without whatever classpath hints, Java volition expression into the electrical current directory in addition to search for com\abc\HelloWorld.class inwards Windows, hence if com directory exists inwards your electrical current directory, your programme volition run otherwise you lot volition acquire "Error: Could non respect or charge top dog shape dto.HelloHP".


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

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


If you lot even hence facing whatever final result only cheque whether you lot receive got accidentally using CLASSPATH surroundings variable, you lot tin give the sack cheque this inwards Windows past times running echo %CLASSPATH% command in addition to inwards Linux past times running echo $CLASSPATH. If CLASSPATH is nonempty in addition to then it volition impress its value otherwise only echo the same command.

4) If you lot are running inwards Java version 1.6 or 1.5, in addition to then instead of receiving "Error: Could non respect or charge top dog class", you lot volition get Exception inwards thread "main" java.lang.NoClassDefFoundError: HelloWorld. It's exclusively from JDK 1.7 onward nosotros are started receiving this novel error. The solution is just same, every fleck of tidings applies to that instance every bit well. So if you lot are non able to solve that occupation past times next steps here, reach permit me know in addition to I volition seek to operate amongst you lot to troubleshoot the problem.

Here is the screenshot of how I tried to reproduce in addition to solve the fault every bit discussed inwards the previous paragraph:

 Could non respect or charge top dog shape HelloWorld Error: Could non respect or charge top dog shape inwards Java [Solved]



That's all almost how to solve "Error: Could non respect or charge top dog shape HelloWorld" inwards Java. Classpath is piffling confusing theme to master, but you lot volition empathise it in i lawsuit you lot started writing in addition to running to a greater extent than or less Java program. If you lot are even hence non able to laid upwards your occupation in addition to then post a comment in that place amongst what you lot receive got tried in addition to nosotros volition seek to troubleshoot together. My destination is non only to give you lot solution but too brand you lot able to explicate why the solution is working in addition to CLASSPATH basics are really of import for a Java developer. I receive got seen many programmers getting frustrated, losing involvement inwards Java due to diverse PATH in addition to CLASSPATH issues e.g. NoClassDefFoundError in addition to ClassNotFoundException in addition to this is my humble seek to convey them dorsum in addition to empower amongst practical knowledge. Hope you lot understand.


Further Reading
Building debugging in addition to troubleshooting science is non slowly in addition to it takes lots of exercise in addition to sense to understand, reproduce in addition to solve the error. If you lot are novel Java developer in addition to then you lot kickoff pick a mass to larn Java from start to end, but if you lot are intermediate Java developer in addition to then you lot should expression at the next resources to railroad train your debugging skill.

Understanding the Java Virtual Machine: Class Loading in addition to Reflection
The Complete Java MasterClass
Core Java for the Impatient - Covers Java SE 8


Sumber https://javarevisited.blogspot.com/

0 Response to "Error: Could Non Honor Or Charge Principal Degree Inwards Coffee [Solved]"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel