Error: Could not find or load main class Akash [duplicate] - java

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 5 years ago.
I want to run selenium program from command line. The code I have is
class Akash
{
public static void main(String args[])
{
org.openqa.selenium.WebDriver driver = new org.openqa.selenium.firefox.FirefoxDriver();
}
}
Filename : Akash.java (Path : C:\Users\anigam\Desktop\Testing)
The required jars are present here : C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib
I compiled the file as :
javac -cp "C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib*" Akash.java
The compilation was successfull and I got Akash.class generated here :
C:\Users\anigam\Desktop\Testing
When I am trying to execute this class file as :
java -cp "C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib*" Akash
I am getting an error :
Error: Could not find or load main class Akash
Can someone please help.
Thanks much.

Your program is able to load dependant libraries from the classpath you provied but unable to load the class file which you compile and generate(Akash.class).
So try adding the current directory where Akash.class resides also in the class path.
1) Open Command prompt
2) cd C:\Users\anigam\Desktop\Testing
3) Running below command would help
java -cp "C:\Users\anigam\Desktop\Order Streaming\Automation_03.04.2017\For_Checkin\uitestframework\lib*;." Akash
Hope it helps.
-Sandeep

Related

Cannot run any Java files due to class not found error [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
What is a classpath and how do I set it?
(10 answers)
Closed 1 year ago.
I'm running into an issue where I am able to compile Java files but I cannot run any of them. I have currently created a Hello World java file in my Desktop folder and compiled it and there is a HelloWorld.class that has been created as well. But when I run java HelloWorld, I get the following error:
Error: Could not find or load main class HelloWorld Caused by: java.lang.ClassNotFoundException: HelloWorld
I have googled around and it seems to have something to do with CLASSPATH? I have tried the command ECHO ${CLASSPATH} and it has returned me nothing but apparently it should return me . ? Could someone point me in the direction of how to fix this java issue and does it have anything to do with CLASSPATH? Thank you!

ClassNotFoundException in JDBC program despite of adding driver's JAR file [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 2 years ago.
I am writing a simple program in Java using to demonstrate insertion of data in MySQL table using JDBC. But the program is generating ClassNotFoundException while registering driver.
import java.sql.*;
public class test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException: "+e.getMessage());
}
}
}
I have added the driver JAR file in the same directory.
To compile the program:
javac -cp ".:~/Programs/D/friends/assignment/driver.jar;" test.java
To execute the program:
java -cp ".:~/Programs/D/friends/assignment/driver.jar;" test
O/p:
ClassNotFoundException: com.mysql.jdbc.Driver
Note: The issue is caused by ; at the end of the driver.jar and also not using fully qualified path.
Windows based OS uses ; separator whereas Unix-based OS uses : separator.
Solution :
First compile the code : javac test.java (Run this command)
Run the code without semi-colon : java -cp .:<fully-qualified-path>/driver.jar test
Sample output :
anish#Anishs-MacBook-Pro ~ % javac Test.java
anish#Anishs-MacBook-Pro ~ % java -cp .:/Users/anish/driver.jar Test
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Note : I'm using mysql-connector-8.0.15.jar. If you are using the same or greater, then change from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver as that class is deprecated.
1.at the end of the classpath there seems to be an extra semi-colon:
/assignment/driver.jar;"
try without it
2. Are you sure driver.jar is the correct file?
normally they are called like mysql-connector-java-8.0.23.jar

Could not find or load main class test [duplicate]

This question already has answers here:
Running java in package from command line
(5 answers)
Closed 8 years ago.
I have already come across this error. I couldn't figure out today also.
package com.example.cassandra;
public class test
{
public static void main(String[] a)
{
System.out.println("test");
}
}
This is my java file. My working directory is
com/example/cassandra
Compile command is
javac test.java
Changed working directory to parent directory of com
cd ../../..
Run command
java test
Says
could not find or load main class test
Any body please explain what's the problem here?
You will need to actually specify the package name:
java com.example.cassandra.test

Playing sound files with Java - Error: Could not find or load main class [Classname] [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 9 years ago.
I'm trying to (do something as 'simple' as) getting java to play a sound file.
I've got the following java-code for it:
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class HelloWorldSound
{
public static void main(String args[]) throws Exception {
String soundFile = "sound.mp3";
Media hit = new Media(soundFile);
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
}
}
(Sound file is in the same directory as the .java and .class file)
I compile it using:
javac -cp jfxrt.jar HelloWorldSound
(The .jar file is in the same folder)
No errors on compilation, but when i try to run it with:
java -cp jfxrt.jar HelloWorldSound
I get the follwing error:
Error: Could not find or load main class HelloWorldSound
I'm running java version "1.7.0_45"
Any help on where i made my mistake(s)?
The overall idea: The program is for a project, where input from an arduino will decide which sound to play and how often it should be repeated. If there is a better way to playback sound, please let me know :)
If you run it like this:
java -cp jfxrt.jar HelloWorldSound
then the only thing that is in your classpath is jfxrt.jar. If your class HelloWorldSound is not in a package and your have HelloWorldSound.class in your current directory, then you need to put the current directory in the classpath too:
java -cp jfxrt.jar;. HelloWorldSound
Note: . indicates the current directory.

Error: Could not find or load main class [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 7 years ago.
I am having trouble compiling and running my Java code, intended to allow me to interface Java with a shared object for Vensim, a simulation modeling package.
The following code compiles without error:
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
However, when I try to run the following:
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
I get the following error: "Error: Could not find or load main class SpatialModel
". My SpatialModel.java code does contain a 'main' method (below), so I'm not sure what the problem is - can anyone please help me out? Thanks.
import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
public class SpatialModel {
private VensimHelper vh;
public static final String DLL_LIBNAME_PARAM = "vensim_lib_nam";
public static final String MODEL_PATH_PARAM = "vensim_model_path";
private final static int VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT = 10;
public SpatialModel() throws SpatialException {
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if(libName == null || libName.trim().equals("")) {
log.error("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
throw new SpatialException("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
}
if(modelPath == null || modelPath.trim().equals("")) {
log.error("Model path has to set with -D" + MODEL_PATH_PARAM);
throw new SpatialException("Model path ahs to be set with -D" + MODEL_PATH_PARAM);
}
for (int i = 0; i < VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT && vh == null; i++) {
try {
log.info("creating new vensim helper\n\tdll lib: " + libName + "\n\tmodel path: " + modelPath);
vh = new VensimHelper(libName, modelPath);
} catch (Throwable e) {
log.error("An exception was thrown when initializing Vensim, try: " + i, e);
}
}
if (vh == null) {
throw new SpatialException("Can't initialize Vensim");
}
}
public static void main(String[] args) throws VensimException {
long before = System.currentTimeMillis();
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if (libName == null) {
libName = "libvensim";
}
if(modelPath == null) {
modelPath = "~/BassModel.vmf";
}
System.setProperty(DLL_LIBNAME_PARAM, libName);
System.setProperty(MODEL_PATH_PARAM, modelPath);
if (args.length > 0 && args[0].equals("info")) {
System.out.println(new VensimHelper(libName, modelPath).getVensimInfo());
} else if (args.length > 0 && args[0].equals("vars")) {
VensimHelper helper = new VensimHelper(libName, modelPath);
String[] vars = helper.getVariables();
for (String var : vars) {
System.out.println(helper.getVariableInfo(var));
}
} else {
File f = new File(".");
System.out.println(f.getAbsolutePath());
SpatialModel sm = new SpatialModel();
}
System.out.println("Execution time: " + (System.currentTimeMillis() - before));
}
}
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder, add . to your classpath.
Note that the Windows classpath separator is a semi-colon, i.e. a ;.
If the class is in a package
package thepackagename;
public class TheClassName {
public static final void main(String[] cmd_lineParams) {
System.out.println("Hello World!");
}
}
Then calling:
java -classpath . TheClassName
results in Error: Could not find or load main class TheClassName. This is because it must be called with its fully-qualified name:
java -classpath . thepackagename.TheClassName
And this thepackagename directory must exist in the classpath. In this example, ., meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which thepackagename exists.
To be clear, the name of this class is not TheClassName, It's thepackagename.TheClassName. Attempting to execute TheClassName does not work, because no class having that name exists. Not on the current classpath anyway.
Finally, note that the compiled (.class) version is executed, not the source code (.java) version. Hence “CLASSPATH.”
You can try these two when you are getting the error: 'could not find or load main class'
If your class file is saved in following directory with HelloWorld program name
d:\sample
java -cp d:\sample HelloWorld
java -cp . HelloWorld
I believe you need to add the current directory to the Java classpath
java -cp .:./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
You have to include classpath to your javac and java commands
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
file is located inside "src/com/test/Hello.java"
from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
In windows the same thing will be working too, I already tried
If you work in Eclipse, just make a cleanup (project\clean.. clean all projects) of the project.
You have to set the classpath if you get the error:
Could not find or load main class XYZ
For example:
E:\>set path="c:\programfiles\Java\jdk1.7.0_17\bin"
E:\>set classpath=%classpath%;.;
E:\>javac XYZ.java
E:\>java XYZ
I got this error because I was trying to run
javac HelloWorld.java && java HelloWorld.class
when I should have removed .class:
javac HelloWorld.java && java HelloWorld
Check your BuildPath, it could be that you are referencing a library that does not exist anymore.
If you're getting this error and you are using Maven to build your Jars, then there is a good chance that you simply do not have your Java classes in src/main/java/.
In my case I created my project in Eclipse which defaults to src (rather than src/main/java/.
So I ended up with something like mypackage.morepackage.myclass and a directory structure looking like src/mypackage/morepackage/myclass, which inherently has nothing wrong. But when you run mvn clean install it will look for src/main/java/mypackage/morepackage/myclass. It will not find the class but it won't error either. So it will successfully build and you when you run your outputted Jar the result is:
Error: Could not find or load main class mypackage.morepackage.myclass
Because it simply never included your class in the packaged Jar.
I know this question was tagged with linux, but on windows, you might need to separate your cp args with a ; instead of a :.
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar;./vensim.jar SpatialModel vars
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
If you try to run a java application which needs JDK 1.6 and you are trying to run on JDK 1.4, you will come across this error. In general, trying to run a Java application on old JRE may fail. Try installing new JRE/JDK.
Problem is not about your main function. Check out for
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
output and run it.
Project > Clean and then make sure BuildPath > Libraries has the correct Library.
java -verbose:class HelloWorld might help you understand which classes are being loaded.
Also, as mentioned before, remember to call the full qualified name (i.e. include package).
I was using Java 1.8, and this error suddenly occurred when I pressed "Build and clean" in NetBeans. I switched for a brief moment to 1.7 again, clicked OK, re-opened properties and switched back to 1.8, and everything worked perfectly.
I hope I can help someone out with this, as these errors can be quite time-consuming.
This problem occurred for me when I imported an existing project into eclipse. What happens is it copied all the files not in the package, but outside the package. Hence, when I tried run > run configurations, it couldn't find the main method because it was not in the package. All I did was copy the files into the package and Eclipse was then able to detect the main method. So ultimately make sure that Eclipse can find your main method, by making sure that your java files are in the right package.
If so simple than many people think, me included :)
cd to Project Folder/src/package there you should see yourClass.java then run javac yourClass.java which will create yourClass.class then cd out of the src folder and into the build folder there you can run java package.youClass
I am using the Terminal on Mac or you can accomplish the same task using Command Prompt on windows
If you are using Eclipse... I renamed my main class file and got that error. I went to "Run As" configurator and under the class path for that project, it had listed both files in the class path. I removed old class that I renamed and left the class that had the new name and it compiled and ran just fine.
This solved the issue for me today:
cd /path/to/project
cd build
rm -r classes
Then clean&build it and run the individual files you need.
I have a similar problem in Windows, it's related to the classpath. From the command line, navigate until the directory where it's located your Java file (*.java and *.class), then try again with your commands.
I use Anypoint Studio (an Eclipse based IDE). In my case everything worked well, until I found out that while running the java code, something totally different is executed. Then I have deleted the .class files. After this point I got the error message from this question's title. Cleaning the project didn't solve the problem.
After restarting the IDE everything worked well again.

Categories