Wednesday 17 April 2019

Classpath | JAVA HOME | Classnotfound | main class not found


In window CMD,

1. When we type any commands, lets say "javac", How CMD will know, where exactly is this tool is located, you may type this in any directory, if you type this command, being inside the directiry where it resides, then it will work, else CMD will not know, where to look for.

Ans:

If you want "sandeep" to be recognized as cmd command by window, then you need to tell where to look for sandeep to window

Windows --> Set environment path --> add variable ,




Now I added "C:\..\jdk\bin\,  exact located path , till "bin", now i can type any tool under, which is under that bin folder, window will recognize it as tool, because, that folder in in the path to look for list of window, Sp the "javac" command will work anywhere in the cmd,



Compile time error:

No class found , No main found


How javac <file.java> performs search

How java <classname> performs search

I have "Test.java" and "Test.class", whose package is -->
package kafka.test.com.kafka_test;

Under this folder(its a maven project)

C:\Users\s.jaisawal\workspace\kafka-test\src\main\java\kafka\test\com\kafka_test

But when I go inside this directory via cmd, and run

C:\Users\s.jaisawal\workspace\kafka-test\src\main\java\kafka\test\com\kafka_test>java Test
Error: Could not find or load main class Test


C:\Users\s.jaisawal\workspace\kafka-test\src\main\java\kafka\test\com\kafka_test>
java kafka.test.com.kafka_test.Test
Error: Could not find or load main class kafka.test.com.kafka_test.Test

So I went back to a directory where package starts ,
C:\Users\s.jaisawal\workspace\kafka-test\src\main\java> java Test
Error: Could not find or load main class Test

Finally , from the start of package directory, I gave fully qualified name of my class

C:\Users\s.jaisawal\workspace\kafka-test\src\main\java>java kafka.test.com.kafka_test.Test
Main is running.....


So how does java seaches the classes while loading or executing ?


How a jar is run, how main in jar is found, why -classpath is used while running  a jar ?


Problem with maven eclipse project


Normal java eclipse project, you can right click on java file and "run as application", eclipse compiles and looks for main and runs, but in maven project,

You have to clean and install the pom.xml and then you can run any java (with main), each modification requires a clean build.


======================Problem with Eclipse ====================================

Issue: Project --> Build , doesn't build anything it show no output, 

Cause: 
Make sure that you create a Java project and that your Java files are in
a source folder of that project.



=============Compile your single java file =======================================

1. In linux/window, find a folder where all jar files are present, typeically ../lib/ and include those lib folders in your class path 


javac -cp /home//lib/*: <java_File_name>.java

Similarly, you can include all different libs type directory with : separated in -cp



 

1 comment:

  1. Harrah's Reno Hotel & Casino - MapYRO
    View Harrah's Reno Hotel & Casino (www.harrahsro.com) location 하남 출장마사지 in 경주 출장마사지 Reno, 포항 출장안마 NV and other nearby cities, revenue, 용인 출장샵 industry and 평택 출장샵

    ReplyDelete