1

i wanted to practice developing database programs in java so i set the classpath to E:\software\installed\java\jre\lib by declaring a new environmental variable classpath and gave value as E:\software\installed\java\jre\lib but now i am unable to run any program(not even non-database) i get an error

Error: Could not find or load main class MysqlConnect

but when i delete the classpath variable i am able to run non-database programs. what is the possible problem and please let me know the solution. i have set path to E:\software\installed\java\bin

nothing is helping i tried using class path switch in java which is not helping (java -cp C:\Program Files\MySQL\MySQL Server 5.5 MysqlConnect) nor do setting path is helping (set CLASSPATH=%CLASSPATH%:C:\Program Files\MySQL\MySQL Server 5.5:C:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar) all are giving the error

Error: Could not find or load main class MysqlConnect

Sar009
  • 2,166
  • 5
  • 29
  • 48

5 Answers5

1

You shouldn't be defining a classpath for your programs using system variables. Standard way is to use the command line -cp or -classpath option. Take a look at what java prints out if you run it with no arguments.

You will find this mentioned in this tutorial. It says:

The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care

Also, see how the default CLASSPATH environmetn variable has a . in it. If you still decide to add your classes in there, keep the . for other citizens and add your classes after a ;, don't just overwrite the whole value.

Pavel Veller
  • 6,085
  • 1
  • 26
  • 24
  • `java -cp C:\Program Files\MySQL\MySQL Server 5.5 MysqlConnect` gives error `Error: Could not find or load main class MysqlConnect` – Sar009 May 22 '12 at 17:06
  • @Sar009, you will have to ensure your `MysqlConnect` is in that folder in its `.class` or `jar` form. And consider enclosing the path with the spaces in it using `""`. Also, your `MysqlConnect` class is likely defined in a package so you would need to refer to it using its "full qualified name", something like `home.sar009.MysqlConnect`. – Pavel Veller May 22 '12 at 19:13
  • i did this `C:\Users\sarad mohanan\Desktop\rose>java -cp "c:\Program Files\MySQL\MySQL Server 5.5" "c:\Users\sarad mohanan\Desktop\rose\MysqlConnect"` but still same error `Error: Could not find or load main class` its not in package i also tried by keeping the `MysqlConnect.class` in `c:\Program Files\MySQL\MySQL Server 5.5` and changed the directory to that path and ran but still no help. i think java is accepting the -cp switch but after that it cant find the `MysqlConnect` class – Sar009 May 23 '12 at 02:37
  • Also what i found is after using -cp switch i cant execute any program not even non-database. it gives error `Error: Could not find or load main class classname`. so we have to solve the problem why -cp switch is not working then we can find the solution for the original problem – Sar009 May 23 '12 at 02:58
1

Here's a jdbc tutorial for beginners that could help you find your answer. If you skip to step 14 it gives an example of how the classpath is used. It is used as the first respondent is saying, by command line.

Hopefully this will help you in your particular situation. This tutorial is not database-specific so it will work with any type of database you are using.

1

ok atlast i got the solution it should be typed as

C:\Users\sarad mohanan\Desktop\rose>java -cp .;"c:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar" MysqlConnect MySQL Connect Example. Connected to the database Disconnected from database

we have to add .; before the original path . holds default classpath. pravel veller had said it previously but i didn't understand it then

Sar009
  • 2,166
  • 5
  • 29
  • 48
0

I would like to propose using a good tool like Eclipse. You can test the CLASSPATH indirectly and your environment by adding or removing jar files and libraries.

Obviously, the libraries in your default CLASSPATH is different than the one in your E drive. You may compare the files by knowing your CLASSPATH. In Windows, do "echo %CLASSPATH%".

Eclipse can help you by experimenting without rebooting or any annoying steps. And this stuff is not easy.

The Original Android
  • 6,147
  • 3
  • 26
  • 31
-1

Crete o set environment variable if you use windows JAVA_HOME=E:\software\installed\java, and after append the Java bin directory %JAVA_HOME%\bin. Note that paths are separated from each other with semicolons (;) I hope help you. That's work for me !!!. I don't understand. What happen?.

hekomobile
  • 1,388
  • 1
  • 14
  • 35