Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Could not find or load main class error

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am attempting to set up communication between this Java 8 program and a MySQL instance.  I have changed the identifiers within the Connection String to generalize what I am using for the connection.

When running the program in the Terminal on Mac, I receive a "Could not find or load main class" error.  To my understanding, I have written this properly.

I'm anticipating answers about the Connector/J and Connection String accuracy but getting the main class to be recognized first is the goal.  Feel free to comment or ask questions about the accuracy of the connection channel (Connector/J and Connection String) but the main class error is my focus for the time being.

About the Connection/J Driver:  I am running this through Terminal with the command "java -cp .:$HOME/absolute/path/to/Connector/J JDBCTestConnection3" less the quotes.

Thank you

 
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did that class compile normally? I so, where is the .class file? Did you give your class a module/package name?
 
Christian Antfeld
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Did that class compile normally? I so, where is the .class file? Did you give your class a module/package name?



I just ran javac on that file and created a class file from it.  There were no errors expressed in the terminal but this is the first time I've ever compiled a .java file before so I'm not sure if it would have thrown one if the .java file had an error.  No, I have not added a package name.  I think I have been reading very poor resources and do not understand the importance of these things outside of "just do it".

What is the correct protocol for assigning a package name? I have no clue how they work because the readings I've done have either assumed understanding of what packages are by the reader or they just say "this is the package name".
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, it appears the class compiled correctly, otherwise you would have had error messages. Please confirm you have that .class file in this location with the dir command (Windows®) or ls (Unix/OSX/Linux).
You probably should have given that class a package name, which you can read about in the Java™ Tutorials, but stick to one problem at a time and don't change anything else until you get your code to run. I presume you have altered the arguments for the connection to keep your details private.
 
Christian Antfeld
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:So, it appears the class compiled correctly, otherwise you would have had error messages. Please confirm you have that .class file in this location with the dir command (Windows®) or ls (Unix/OSX/Linux).
You probably should have given that class a package name, which you can read about in the Java™ Tutorials, but stick to one problem at a time and don't change anything else until you get your code to run. I presume you have altered the arguments for the connection to keep your details private.



Correct, I changed the details to remain private.  This isn't a big time project or anything but feel it is good practice.  I'm not sure which location you are referring to but the .class file has saved to the same folder as the .java file.

Would a missing package name stop the .class file from running properly?  I would like the main method to run properly but I am left with no direction to work toward.  I'm not seeing anyone say the source code is wrong leading me to believe the code is correct.  I am now stuck at this point.
 
Sheriff
Posts: 6381
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried to compile and execute your code, and I was able to get it to run.  So your code is not the problem.

* Are you CD'd into the directory with the JDBCTestConnection3.class file?
* While in the directory, do you issue the command java JDBCTestConnection3 ?
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Christian Antfeld wrote:. . . I changed the details to remain private. . . . it is good practice.

Yes, that's what I expected; you wouldn't want to publish your private folder details.

. . . the .class file has saved to the same folder as the .java file.

Confirming that you have JDBCTestConnection3.class in the . directory. I presume your connector/J is in the classpath quoted, and you didn't include the slash before the last J. You would appear to be on a Unix‑like system, which would interpret a slash in “Connector/J” as a different directory. Please also check that you don't have any files with a name similar to JDBCTestConnection3 anywhere else in the classpath.

Would a missing package name stop the .class file from running properly?

No. That is why I said not to declare a package name at present.

. . . I'm not seeing anyone say the source code is wrong . . .

If you look at the Java™ Tutorials, you won't find any mistakes, assuming what you have obscured was correct.
I am afraid that apart from what I have suggested before, I can't see any other errors. Maybe somebody else will. You may have to resort to desperate measures: comment out all the code in the main method and change it to a hello world sort of thing.Compile and run that code and see what happens. If it runs, gradually comment the code back in and compile it again.
 
Christian Antfeld
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I just tried to compile and execute your code, and I was able to get it to run.  So your code is not the problem.

* Are you CD'd into the directory with the JDBCTestConnection3.class file?
* While in the directory, do you issue the command java JDBCTestConnection3 ?



1. I'm not sure if I was in the directory with that .class file but I will make sure I am and retry it.

2. No, the tutorial being used instructed "java -cp .:$HOME/path/to/ConnectorJ JDBCTestConnection3" as the Terminal command verbatim.  I am not finding anything within the tutorial or online explaining the -cp argument to the java command either making it more frustrating.

I will try to run the program using "java JDBCTestConnection3" only to see what happens.

Update:

I have moved the Connector/J into the same folder as the .class file and run the java JDBCTestConnection3 command and argument. "No suitable driver found for *connection string here*".
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So have you solved one problem (well done ) and got another problem? Have you got rid of the can't find main class exception?

The -cp option tells the runtime (here the java tool) where to find the resources it requires. If you moved the resource and didn't alter the URL text in the code, then the resource won't be in the expected location, and you will suffer an exception. Have a look at this tutorial about the classpath. Also tell us the full name of the ConnectorJ file; has it got an extension?
 
Christian Antfeld
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marshal Ritchie!  The name of the Connector/J is: mysql-connector-java-8.0.18.jar.  I'm not sure what is meant by URL text in the code.  More precisely, which code is being referred to as in Java code or the Terminal command.  The compiled Java class file is in the same folder as the Connector/J.

To show the logic as I understand it:

1. Java class file written and compiled
2. Connector/J invoked with the -cp argument to java runtime tool
3. Java class file invoked and driven by the Connector/J identified in the Terminal argument
4. Output of SQL query or Exception thrown and error message displayed

To me, the error message doesn't make sense to be main method not found if the Connector/J class path is the issue.  Would it not be that the drive is the source of the issue and represented in the error message if it is in fact the problem?
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Christian Antfeld wrote:Thanks . . .

That's a pleasure

The name of the Connector/J is: mysql-connector-java-8.0.18.jar.

I am never quite sure about this, but does your classpath entry (as set with -cp) end “jar”? If you expand $HOME, wouldn't you need a classpath like /home/cantfield/jdbc/resources/mysql-connector-java-8.0.18.jar? That would appear to be what the Connector J manual says. Start with $HOME or /home and end with .jar. You are correct to include .: in the classpath.

I'm not sure what is meant by URL text in the code.

Look at the DriverManager#getConnection() documentation. The first parameter is called url. This connector J manual page shows an example where the url appears in green on line 9. I presume the ? is part of their database name and you probably don't need it yourself. Don't follow the example with Class.forName(...) in.

. . .  The compiled Java class file is in the same folder as the Connector/J.

I am not sure it is a good idea to move that .jar file about because any changes can cause more confusion. You would now need a classpath something like .:./mysql-connector-java-8.0.18.jar
I presume when you got the no suitable driver error message you didn't get a no main class found error as well.

. . .
1. Java class file written and compiled
2. Connector/J invoked with the -cp argument to java runtime tool
3. Java class file invoked and driven by the Connector/J identified in the Terminal argument
4. Output of SQL query or Exception thrown and error message displayed
. . .

Afraid you have sort of got 2 and 3 the wrong way round. The main method in the named Java® class is invoked first. That has a dependency on a .jar file which you name in the -cp option so the JVM can find the path to that .jar. The main method uses the connector, not the other way round.
I hope i have helped and not caused more confusion.
 
Christian Antfeld
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marshal Ritchie,

You have helped a lot.  What I learned is not to deviate from the tutorial with my limited knowledge of the concepts.  I think I'm going to start over completely and work the tutorial exactly as it says using the $HOME path and have the .java and .class files in the $HOME folder as well.

When I'm finished with it, I'll come back and report on the effects of this.  I appreciate the helpful attention from you and all the others.  I'll be back!
 
Campbell Ritchie
Marshal
Posts: 66266
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Christian Antfeld wrote:. . . helpful attention from you and all the others. . . .

We try our hardest to help, and it is nice to know we are appreciated. Please note the other links I gave you; they may be helpful too. Hope to see you back with better news
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!