Java could not find or load main class error - java

For some reason, I can't get java to run my program. Whenever I try I get the message
"Error: Could not find or load main class Project"
In Command Prompt I type cd Documents since the file is in my Documents folder, type
javac Project.java
then
java Project
to try and run it but I get the above error message.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Project
{
public static void main(String[] args)
{
Code and stuff
}
}
There's a fair bit of code that I left out but I think this is the part that's messed up. Let me know if you need to see the rest of the code and I'll edit this.

Change
java Project
to (assuming Project.class is in your current folder)
java -cp . Project
as it is, you aren't setting a class-path.

You have add the path of .class files in classpath during execution
Run following command:
java -classpath C:\Users\DELL\Documents Project

Related

VSCode Import from JAR file cannot resolve class

We have a java app that needs to use a public class form a JAR file. After much frustration with the main application, we have created a simple repo here to try to figure out what is going on.
The overly simple file that ends up in the JAR file is as follows:
package com.mystuff.helpers;
public class printStuff {
public void showMsg(String msg) {
System.out.println(msg);
}
}
We create the JAR file with this command:
jar cvf MyJavaHelpers.jar com
The folder structure is as follows (the printStuff.java file is in the helpers folder):
A listing of the JAR contents is as follows:
jar tf MyJavaHelpers.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mystuff/
com/mystuff/helpers/
com/mystuff/helpers/printStuff.java
com/mystuff/helpers/README.md
Finally, the program that we have to use this simple class is as follows:
package com.mystuff.testapp;
import com.mystuff.helpers.*;
// To build the JAR file
// jar cvf MyJavaHelpers.jar com
// To display the contents of the JAR file
// jar tf MyJavaHelpers.jar
public class testDriver {
public static void main(String[] args) {
System.out.println("Starting testDriver");
com.mystuff.helpers.printStuff ps = new com.mystuff.helpers.printStuff();
// testPrintStuff(ps);
// testPrintStuffAgain(ps);
}
/*
private static void testPrintStuffAgain(printStuff ps) {
ps.showMsg("This is a fine kettle of clams");
}
private static void testPrintStuff(printStuff ps) {
ps.showMsg("This is a fine kettle of fish");
}
*/
}
In VS Code (v 1.55.0) We have a Java Project to contain our TestDriver that looks like this:
Finally, the issue is that when we try to run the test driver, we get the message:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
com.mystuff.helpers.printStuff cannot be resolved to a type
com.mystuff.helpers.printStuff cannot be resolved to a type
at com.mystuff.testapp.testDriver.main(testDriver.java:15)
We have tried the command Clean Java Language Server Workspace which seems to indicate that it works, but we cannot get past this error.
Based on what we have looked at, the JAR file appears to be in the correct place (It is in the lib folder of the main app).
The import com.mystuff,helpers.; line does not show as an error, so it seems to us that it is found, however, the actual import of the printStuff class fails. We have tried the fully qualified class name as well as relying on the import and only using the short name. They both fail.
We have seem some guidance about setting the classpath, but have not been able to find how to do that explicitly in VS Code.
Of course, if we do not have this little helper in a JAR file, but just as a side-by-side in the same project, it works just fine. The issue that started us down this journey is trying to use a public class from a pre-packaged JAR file.
Any and all help is greatly appreciated. Thanks in advance.
Before adding the jar to library, you may run the command java -jar printStuff.jar to test if it could be executed successfully.
The error occurs because the class must be called with its fully qualified name. To be clear, the name of this class is not printStuff, It's com.mystuff.helpers.printStuff, so the right command should be:
Turn to the folder;
Compile .java file: javac com\mystuff\helpers\printStuff.java
Generate .jar: jar cvfe printStuff.jar com.mystuff.helpers.printStuff .\
Then readd it to referenced libraries and see if the error goes away.

java class executing on Eclipse but not in command line

My java class files run in Eclipse but not in command line. I have tried all possible solutions. My code has the following structure:
Client_1/src/filedownload/Client.java
RMI_interface/src/filedownload/Hello.java
The Client.java file is dependent on Hello.java. filedownload is the name of package.
When I compile using the following command, it works.
javac RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
But when I execute the class file in the Client_1/src folder using following command, it does not work.
java filedownload.Client
The error displayed is
Could not find or load main class
I have tried many posts on stackoverflow but I am unable to solve it. I am using ubuntu.
The code structure is
package filedownload;
import ....
public class Client implements Hello, Runnable{
...other functions.....
public static void main(String args[])throws Exception{
}
}
Does your Client class have the main() method ? Where are the .class files after compilation (that is, what's the current directory you're executing the compile from) ? What's the current directory when you try to execute ? What's the classpath when you try to execute ?
Without all that info, there's little chance of anyone being able to get you going (but for the obvious advice of just setting up eclipse and doing everything from within eclipse - letting eclipse take care of all the nitty gritty detail).
(And the questions themselves suggest various possible points of failure in your scenario so look at it.)
All your steps seems to be correct. You didn't share the Client.java code which has main method.
Make sure you follow this main method syntax:
public static void main(String[] args){
...
}
E.g. if you write main without args, it can't be found.
You need to put your classes in a separate folder, separated from your sources.
javac -d bin RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
(folder 'bin' must exist already)
And inside folder 'bin' execute command:
java filedownload.Client

Running a Java code in cmd is not working while everything is fine in Eclipse

I wrote the code below in Eclipse and trying to do the same in cmd. However I in cmd the error message :
Error: Could not find or load main class GetMousePosition.
In cmd, I am in the folder where they class is : c:\Java\Examples\src\Robots\
When I compile the class (doing : javac GetMousePosition), everything works fine.
Originally, I set up my java's bin folder path in the environment variables.
Thanks in advance for your help
package Robots;
import java.awt.*;
import java.awt.event.*;
public class GetMousePosition {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(MouseInfo.getPointerInfo().getLocation());
}
}
You need to provide the full class name on the command line, in this case Robots.GetMousePosition. java may also assume that your class is in the right directory structure, so you should probably move up one directory.
You need to back up one directory (to c:\Java\Examples\src\) and then run
java Robots.GetMousePosition
The problem is that you need to specify the main class. Eclipse can not find the class that has a main method.
For this you need to
Right click the file in the view -> run as -> Run configuration and there you need to select the class that has main method.
For detailed guide follow this: Set Eclipse launch configuration

Running java helloworld

I tried to google this, went to oracle.com and read all the questions on this forum related to this. I wrote a simple "Helloworld" program
package helloworld;
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
NetBeans compiles the source code into a .class file. I move that file to C:\MyJava
I try to run it by C:\MyJava> java -cp . Helloworld and all possible variations of such. I keep getting the NoClassDefFoundError: Helloworld (wrong name: helloworld/Helloworld).
To make sure. There's a question almost exactly like this (difference in "wrong name"). The solution to that question does not work in my case.
You get the "wrong name" error because your class is in the package helloworld. Java expects you to provide the fully-qualified class name on the command line:
C:\MyJava> java -cp . helloworld.Helloworld
The directory structure must match the package structure. This means that you should have a directory C:\MyJava\helloworld that contains the class file Helloworld.class.
You need to tell it the package name (which is helloworld):
C:\MyJava> java -cp . helloworld.Helloworld
Below post is similar to your problem. I hope it guides you;
How do I run .class files on windows from command line?

How to compile a java program from directory?

I'm learning java and I would like to know how to compile a java program from other directory.
For example, my compiler is in my drive c:\ and I want to compile my java program from drive e:\
How should I do that?
I'm getting this error, what does it mean?
The current directory should be in the default CLASSPATH, but maybe it's not. Try java -cp . Assignment
It's been a while since I've done java, but it seems like the compiling isn't your problem. Since javac returns properly, it seems to be a problem with Assignment.java. Does your Assignment class have a main method?
Well the easy way is to set ur classpath variable. Since from screen shot it seems ur using windows i suggest u right click the my computer nd select properties. Go to advance setting and click environment variable tab.
Then a new window pops up which has System Variable at bottom. Select new and create a variable
JAVA_HOME = path where u installed java eg for me its c:\java
Now once u add this search an existing variable path and choose edit. Now at the end append the following ;%JAVA_HOME%\bin
Now ur done u cn run java program frm any location on ya comp....
You are using package in your code...so it shows NoClassDefFoundError when you run
you should create folder which contain your package name...compile that java file...and you can run that file from previous directory of that java file directory...
For example your code is
package test;
class Assignment{
public static void main(String args[]){
System.out.println("Hai");
}
}
it saved on this path "E:\java\test"
compile this file and you can run this file from this path "E:\java"
command to run this file
java test.Assignment
E:\java> java test.Assignment
Is there a package declaration at the top of your Assignment.java? If so, remove it and recompile for a quick fix.
To work with Java packages, you'll need a directory structure that matches the package declarations.
For example, say this is your Assignment.java:
package myjava;
public class Assignment {
public static void main(String[] args) {
....
....
}
You run this command to compile:
E:\java>javac -d . Assignment.java
And you get myjava\Assignment.class if all went well. The -d . option means "place generated class files in the current directory". javac creates the package hierarchy as directories for you.
Now that your directories match your packages, this should work:
E:\java>java myjava.Assignment

Categories

Resources