Error compiling simple Java File: ClassNotFoundException - java

What can be the problem when I'm compiling Tareena.java file in my C:/FindTheWallet/Proj/ folder? I am getting ClassNotFoundException.
Here is my code below:
class Tareena
{
public static void main(String args[])
{
System.out.println("Still working on it ? ");
}
}

The reason for your problem is Tareena.class is not in your classpath when you are running your program. Follow this link

Compile your class first using javac Tareena.java and then execute it using java Tareena

Related

What's different between run code - run java vscode? And error Could not find or load main class? And package?

I am coding java on vscode. I have source folder:
>...
>lib
>>src
exam1.java
exam1.class
>>Month10
app1.class
app1.java
With:
exam1.java
public class exam1 {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
app1.java
package Month10;
public class app1 {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
I want to ask about why I need to use package Month10 in here. And another, exam1.java run well(both run code and run java), but in app1.java, I only can "run java", can't "run code" (Ctrl+Alt+N in vs code), it exists error Could not find or load main class app1, I think because the command package? (I saved file before running), can anyone help me, thanks.
The Code Runner extension sometimes has problems running code under multiple levels of folders. For a better coding experience, please use the official extension to run the code.
Tips:
Run Code is provided by Code Runner
Run Java and Debug Java are provided by the official extension
You can ask questions about the Code Runne here.
reload VS Code window or F1 ->Clean the java language server workspace. try this could help

Could not find or load main class in a simple java project

I have created a simple java project "DesignPatterns".
I created a file FacadePattern.java with the path being ~/DesignPatterns/Structural/FacadePattern/FacadePattern.java
My FacadePattern.java class,
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But my editor (VSCode) gives me an error at the start of the file:
The declared package "" does not match the expected package "Structural.DecoratorPattern"
So upon clicking on quick fix, it added package Structural.FacadePattern; in the first line.
So the final code became
package Structural.FacadePattern;
public class FacadePattern {
public static void main(String args[]) {
// Some code
}
}
But when I compile the above file using the command
javac FacadePattern.java && java FacadePattern
It is giving me the following error:
Error: Could not find or load main class FacadePattern
Caused by: java.lang.NoClassDefFoundError: Structural/FacadePattern/FacadePattern (wrong name: FacadePattern)
After searching a lot in the internet, I ran the following command:
javac -sourcefile ~/DesignPatterns FacadePattern.java && java FacadePattern
But no use, I am getting the same error.
Can anyone explain why my editor gave an error but code ran successfully before? and why wont it compile after adding "package Structural.FacadePattern;" line? and what is -sourcefile parameter in javac command? and how to run the code with successfully without errors in editor as well as the terminal?
Stick to naming rules. Package names should be lowercase only. In your case, it should be package structural.facadepattern;
Run the 'correct' class. Because your class is not inside 'the base folder' aka the 'default package', you have to prefix the class with its package name: java Structural.FacadePattern.FacadePattern or rather java structural.facadepattern.FacadePattern if you use the proper case for packages.

Simple Java code gets "Could not find or load main class JavaBatchInserter.class"

I have:
public class JavaBatchInserter {
public static void main(String []args) {
System.out.println("Hello World");
}
}
I go to the pwd where my file, named JavaBatchInserter.java, is located and run:
java JavaBatchInserter.java
and get:
Could not find or load main class JavaBatchInserter.java
It's the simplest thing and I can't make it work. ): Help please! Thanks!
Note: I don't want to create a project and a bunch of directories if possible.
Firstly, compile the file JavaBatchInserter.java
javac JavaBatchInserter.java
Then give your full class name to JVM for execution
java JavaBatchInserter
You run the command java JavaBatchInserter.java
It is wrong.First compile the class by running javac JavaBatchInserter.java
The try to run by java JavaBatchInserter

New to Java , error already

package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
I keep getting this error when i run it
Error: Main method not found in class day1.examples.String2, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
and on line error it says type mysmatch cannot convert from java lang string etc..
Thank you
It works for me:
[steve#newbox ~]$ cd /tmp=
[steve#newbox tmp]$ mkdir -p day1/examples
[steve#newbox tmp]$ cat > day1/examples/String2.java
package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
[steve#newbox tmp]$ javac -classpath . day1/examples/String2.java
[steve#newbox tmp]$ java -classpath . day1.examples.String2
HelloAndrei Vlad
[steve#newbox tmp]$
The most likely explanation is that you have managed to get Eclipse rather confused.
My initial idea was that this was a homoglyph problem. But provided that you have copy-and-pasted the code correctly, the evidence disproves that.
The other idea I had was that you had mistakenly created your own version of the String class (in the day1.examples package). However, that should have resulted in compilation errors in the initialization of x.
I'd start by trying with a clean of the project. Seems weird you would need to do so with only one class in a single package but it will recompile all of your code for you. Everytime I have an issue that I don't think I should have I always F5 (refresh) my packages and then clean. You'd be surprised how many issues it fixes.
Go to Project --> Clean
Then try running again.
The code says that there are somethink wrong with your main class but all seems ok.
In Eclipse rigth click on your projects-> run-> run configurations and check if all is ok.

Error: Could not find or load main class Hello2

I'm just learning java and following a book.
I have a program written via text editor and run commands via cmd.
I've complied 1 program thru javac and executed thru java no problem. (Hello)
Then I modified that to add a comment to the class, named file Hello2.java. I compiled it with no problems, but upon execution, I receive this error: Could not find or load main class Hello2.
I have classpath and path set correct;y on environment variables.
Ideas?
UPDATE
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Hello2.java
//Filename Hello2.java
//Written by
//Written on
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*This class demonstrates the use of the println() method to print the message Hello, world! */
I've found the solution to my problem. I know it's not a code problem. But what I did is that I deleted CLASSPATH from system variables and everything now works...at least for now.
Thanks a lot everyone for your inputs, much appreciated!
You have to change the name of the public class too when you change the name of the file. So, if your file is called Hello2.java, the class should be called Hello2 and not Hello.
Are you sure you set the classpath correctly? Why don't you try running java -cp the directory of the .class file Hello ? If that doesn't work please upload the full stacktrace.
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;
If your class file is saved in following directory with Hello2 program name
d:\sample
java -cp d:\sample Hello2
java -cp . Hello2
I believe you have Hello2.java file as below.
class Hello {
public static void main (String args[]) {
System.out.println("Hello");
}
}
Change it to
class Hello2 {
public static void main (String args[]) {
System.out.println("Hello");
}
}
The change is class Hello2 instead of class Hello.
Note : You should always have classname and file name SAME.
Good Luck!!!
Update 1
Are you doing below steps??
Wrote Hello.java
Compile by javac Hello.java
Run by java Hello
Rename Hello.java to Hello2.java
Rename class name i.e. class Hello to class Hello2
javac Hello2.java
java Hello2
I believe you are missing Step 6 & executing step 7 after step 5. Please confirm.

Categories