• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Could not find or load main class

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have the iBanTest.java and iBanTest.class in the same directory (F:\) and I want to execute it using Java command so I can use this in Jenkins. But I get the error "Could not find or load main class". Can someone help me fix this erorr.

PS F:\> java demo.selenium.iBanTest
Error: Could not find or load main class demo.selenium.iBanTest
PS F:\> java iBanTest
Error: Could not find or load main class iBanTest


 
Saloon Keeper
Posts: 13257
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must use the -cp switch to specify the location of your class files. Did you compile them?
 
Master Rancher
Posts: 4465
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classpath must point to the folder containing the demo folder.  If the -cp command is NOT used, then the current directory needs to contain the demo folder.
Otherwise use the -cp option as Stephan said.

The java command must refer to the full class name like you did:   java demo.selenium.iBanTest
 
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because your source file is in a package, as it should be, (as only the silliest toy programs and examples to cut-and-paste to a forum for others to run should ever be in the un-named package with no explicit package statement).

Java expects to see your class file in the directory that corresponds to the package, for:
package demo.selenium;

this would be at demo/selenium/iBanTest.class

This is true whether you are running from a Jar or just right out of your directory structure.

There are two different ways to achieve this (two recommended ways, you could also do various goofy things that would also work).

1. If all of your .java source files are already inside the appropriate directory structure, you could just compile them "in-place", something like:
javac demo/selenium/iBanTest.java
This will compile your source file into one or more .class files in the same directory that the .java file was in at the time of compiling.  This should normally be in the folder corresponding to the package statement for the source, as shown above.
The javac command should be typed at the directory which contains demo/selenium directory.

2. To place your .class files in a separate directory structure to be run from, or jar'd from, you would instead use the -d option of javac
If you do this, each .class file will be placed under the directory structure corresponding to its package name under the directory named in the -d option, regardless of where the .java source file sat.

It was hard to find an example I had that used packages but no modules, but I found one:
PS F:\Java> cat StringThings/src/com/jesse/practice/StringTest.java
package com.jesse.practice;
...

I compiled it to -d F:Java/StringThings/bin
then I go there and run a java command of your form which works its magic, finding the .class file in the location implied by the package statement:
PS F:\Java\StringThings\bin> java com.jesse.practice.StringTest
toString() from same Double value inconsistent
subString() from String inconsistent
subString() from StringBuilder inconsistent
Problems 3 and 4:

 
Jesse Silverman
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If running the java -classpath command, make sure that your value for classpath contains the root of your package structure, that is you need to do something like this:
PS F:\Java> java -cp F:\Java\StringThings\bin com.jesse.practice.StringTest
toString() from same Double value inconsistent
subString() from String inconsistent


not like this:
java -cp F:\Java\StringThings\bin\com\jesse\practice com.jesse.practice.StringTest
Error: Could not find or load main class com.jesse.practice.StringTest
Caused by: java.lang.ClassNotFoundException: com.jesse.practice.StringTest
 
Jesse Silverman
Saloon Keeper
Posts: 1277
38
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't specific to the error you were having, but don't place files like this directly under F:\, you will create a real mess.

My example isn't a great one to follow either, because it seems like I have installed the JDK there from the poorly chosen name.

Pretend my examples all said F:\JavaSources instead.
 
John Herrington
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will I be able to run this program from anywhere after its compilation? What all files do I need if I need to run this from any system? I believe just the .class file is enough? It has selenium driver jar dependency. I have a selenium server where I want to execute this class file so it runs from within the server. I can copy the whole src folder if needed.

 
Saloon Keeper
Posts: 24295
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, don't start class names with a lower-case letter. Java may allow it, but it's not recommended practice and some Java tools will get confused. Classes begin with upper-case. instance, property and method names start with a lower-case letter.

Secondly, the java command requires the fully-qualified class name. So the correct command is:

That's based on a scheme where you compiled with a command line something like this:

(Correct me if I screwed up, Jesse!)

I'm basing this on a project directory where the current directory contains src and classes sub-directories.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic