Could not find or load main class test [duplicate] - java

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

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

Javac is making all names of my code into myclass.class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I was just trying to compile my java code then I did this in the cmd:
chdir C:\All_files (my directory)
javac Hello.java
then when I did this:
java Hello
오류: 기본 클래스 hello을(를) 찾거나 로드할 수 없습니다.
(Error: cannot find class hello)
then I did this:
javac myclass
then it worked.
How do I fix that?
Here is my code:
public class Hello
{
public static void main(String[] args) {
System.out.println("Hello Windows 10");
}
}
I want to know how do I make javac produce the right title for the compiled class.
What do you want to fix?
First you compiled your source file with javac Hello.java...
Then you tried to run it with java Hello...
However the command java requires a fully qualified class name. What you supplied (Hello) seems to me, like the name of he .class file, without extension.
When you tried with java myclass, it worked, because it is the fully qualified class name of your class...
See: java and javaw reference

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

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

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.

Categories