cannot run java .class file from cmd [duplicate] - java

This question already has answers here:
Could not find or load main class package.class
(3 answers)
Closed 7 years ago.
I have a java project with main class
in this path:
C:\Users\tomer_000\Desktop\eclipse_tmp\
I have followed this tutorial
C:\Users\tomer_000\Desktop\eclipse_tmp\src\tomer1>javac Test.java
I have checked and there are now two files:
Test.java, Test.class
I now try to run:
C:\Users\tomer_000\Desktop\eclipse_tmp\src\tomer1>java Test
and get this error:
Error: couldn't find or load main class Test
I have defined PATH and CLASSPATH on my win8.
I have tried:
PS C:\Users\tomer_000\Desktop\eclipse_tmp\src\tomer1> java tomer1.Test
Error: Could not find or load main class tomer1.Test
how can i fix this?
this is my code:
package tomer1;
public class Test {
public static void main(String[] args) {
System.out.print("hi");
}
}

Go one dir up and run java with a package name:
C:\Users\tomer_000\Desktop\eclipse_tmp\src>java tomer1.Test.java

Related

Exception java.lang.ClassNotFoundException when importing a class in my local dir [duplicate]

This question already has answers here:
How do I resolve ClassNotFoundException?
(28 answers)
What does "Could not find or load main class" mean?
(61 answers)
How to use Java packages? [duplicate]
(3 answers)
Package in Java
(6 answers)
Closed 10 months ago.
I created two files in the same directory.
One is called VMAPI.java
package VMAPI;
public class VMAPI {
public VMAPI() {
System.out.println("Created VMAPI");
}
}
The other one is called Main.java and looks like this:
import VMAPI.VMAPI;
public class Main {
public static void main (String[] args){
VMAPI vmapi = new VMAPI();
}
}
I compile this using the command
javac *.java
And Run using
java Main
When I do, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: VMAPI/VMAPI
at Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: VMAPI.VMAPI
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
I understand the error is telling me that it can't find the VMAPI class, but I don't understand why.
All I want to do is create maybe 2 or 3 classes for a very simple example use in my main function and that's it. I don't wan to use an IDE and want to compile, preferably with the command line.
As per the Java recommendation, you should not have this type of naming convention.
Package name should be in lower case and class name should start with capital letter (camel) and try to have different names.
for example:
package name - vmapi
class name - VmapiClass (this is just an example but you know the context)
then your import should be like this.
import vmapi.VmapiClass;

Freshly downloaded eclipse showing error upon completing "hello world" tutorial (java) [duplicate]

This question already has answers here:
InvalidModuleDescriptorException when running my first java app
(8 answers)
Closed 2 years ago.
Entered code when followed through tutorial.
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("test");
}
}
This is the outcome of my code
Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: C:\Users\me\Desktop\CIS129\Eclipse files\HelloWorld\bin
Caused by: java.lang.module.InvalidModuleDescriptorException: HelloWorld.class found in top-level directory (unnamed package not allowed in module)
Deleting module-info.java might solve it. If it doesn't, then try creating a package and putting your code in that. Eclipse's interface should help a lot with the process of making a new package.

Why is .class file not executing? It's getting created when I use java compiler [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 3 years ago.
Is it because of environment variables? I am getting the following messages.
C:\Users\Manas Sharma\Desktop\java>javac fjp.java
C:\Users\Manas Sharma\Desktop\java>java fjp.class Error: Could not
find or load main class fjp.class
import java.lang.*;
class fjp{
public static void main (String[] args) {
System.out.println("Hello World");
}
}
You can use
java -classpath . fjp

Java.lang.ClassNotFoundException error cmd [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
Hi i've compile this java project. This is my first project of hello world but when i try to run it on cmd i have this error. Can you help me?
This is my code:
public class CiaoMondo {
public static void main(String[] args)
{
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
This is the error:
A ClassNotFoundException can be caused for several reasons. The most common (in my experience) are because the java ClassName command is blocked by your firewall, or the file your class is in is improperly named.

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

This question already has answers here:
Eclipse "Error: Could not find or load main class"
(61 answers)
Closed 9 years ago.
Writing a simple program in Eclipse Kepler I am getting the following error:
Error: Could not find or load main class
I already tried the options listed here, though the options the answers suggest do not seem to exist in Kepler. For example, there is no 'build Automatically' button to check as the accepted answer suggests. Here is the photographic proof:
Code is as follows:
public class keplertest {
public static void main(String[] args){
System.out.println("hello");
}
}
Unable to 'add' the main class under the 'projects' heading either.
Your main method should be:
public static void main(String[] args)
the String[] args is required as that is the method signature Java looks for when launching your Java program
For more information why String[] args is required: Why is String[] args required in Java?
Also, the "build automatically" reference is pointing to the project menu bar item, not in the window.
From the same Properties window Select Run/Debug
If your launching class doesn't exist in that list, you need to add it by clicking New - Java Application, then in the dialog Search for your class in the Main Class text field. That should give your project a Main launching class
And/OR try to go to Run as -> Run Configuration from the project context menu. In the Main Class text field put in the fully qualified name of the launching class
You may need to clean an build afterwards

Categories

Resources