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?
Related
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
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
I looked around, but none of the solutions that worked for others worked for me.
I installed java 1.8.0
My path variable is C:\Program Files\Java\jdk1.8.0_05\bin
I try to run the following program hello.java:
package hello;
public class hello{
public static void main(String[] args){
System.out.println("Hello");
}
}
The program compiles fine when I run javac hello.java
But when I use java hello , java -cp . hello, or java -classpath . hello it returns the error 'Could not find main class hello'.
I know this is a very basic problem, but I really can't figure it out.
Thanks in advance
In very similar answers that I've provided, provided that you're compiling in the current directory, then you need to ensure that your compiled class has made its way to a folder called hello/.
If it has, then you can run this:
java -cp /path/to/hello hello.hello
The above adds the hello/ folder to the classpath, and then you can run the main class using its fully qualified name.
Here you define package so you cannot run directly your compiled class because JVM is not able to find your class so you have to write the path of your hello directory in run command.
for example:
java -cp c/workspace/hello hello
I try your code. And see what the problem is. If you put the "hello.java" in a folder named "JavaTrials" and compiled it there as "javac hello.java", it compiles and produces "hello.class" right there. This command does not produce a folder named "hello" for the package.
Your compiled code "hello.class" should be in a folder named "hello" which is the name of the package. And then you have to run the command "java hello.hello" not from inside the folder "hello" but from the containing folder.
The better way is to put your code in a folder named "hello" before compiling it. This folder represents the package. Then compile it from the outside of the "hello" folder with command "javac hello/hello.java". And then you can run it by "java hello.hello"
As a side note, in java coding tradition class names begins with upper case. Se better use "Hello" instead of "hello".
remove first line "package hello;"
I am new to Java and I got this book to help me start.
I have successfully compiled Hello.java using "javac Hello.java".
Now it says to type in "java Hello" and I am getting "Could not load of find main class Hello". I have tried to find out how to fix it before but all the answers are complicated and confusing. If someone could explain how to fix this, that would be awesome.
The problem appears to be of CLASSPATH.
Solution 1
Add the path of your directory where you have compiled your class to the CLASSPATH variable in Environment Variables.
Solution 2
Every time you run the program add the current folder and libraries you are referencing in the classpath using -classpath. eg:
java -classpath .;lib/referenced-libs.jar my.package.MainClass
Make sure the main method with exactly this syntax is present in the Hello.java file:
public static void main(String[] args) {
// your code will go here...
}
you should check if the hello.class file is present in the folder. java runs these .class files. These files are created on successful compilation.
I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again.
I have two simple classes,
package One;
import One.Inner.MyFrame;
public class test
{
public static void main(String args[])
{
MyFrame f= new MyFrame();
}
}
And the other class is,
package One.Inner;
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
setPreferredSize(new Dimension(400,560));
setVisible(true);
}
}
I am at base folder "basic" in Windows cmd. I compile using
basic> javac *.java -d .
A folder and subfolder is created.
cd One
basic\One> java test
This generates a big set of errors. Many answers directed to specify the full path which didn't work.
My classes are in One so specifying One using -cp didn't work either.
You'd run it as:
java One.Test
... but from the root directory (basic), not from the One directory. You always specify the fully-qualified class name.
Oh, and package names in Java should be lower-case, so it should be one and one.inner, not One and One.Inner. Just a convention, but one which pretty much everyone follows.
If the directory is:
basic\One
Run java from the base directory of the package:
basic>java One.test or basic>One.test <optional arguments>
(ideally the package would be lowercase and the class upper case):
basic>java one.Test
If you get 'does not exist' messages, then the java command cannot find classes you referenced in your class. You can point to them with the -cp option ('.' means 'here', and you can add as many places as you like divided by ';' on Windows and ':' on Linux).
basic>java -cp . one.Test
or
basic>java -cp .;..\..\someJar.jar;c:\someDirectory\classesDirectory one.Test
The following line of Haralan Dobrev code solves the problem.
java -cp ../ one.Test
while creating a class with a package if you want to run it from cmd you must created a directory with same name of package put the .class in it and then you can easily run it for example you created a class with name "one" and this class in the package with name pack ,you must run these commands
1 javac one.java
after compilation created a directory with the name pack ,after that run this command
2 java pack.one
Note:
all this must be done in the current working directory and the name "one" i chose it here as file name and main class name
we all know the first name used in the first command is file name and second one is main class name
This is because if you are declaring package in your java file, then JAVA compiler believe you are having same folder architecture in your system.
In your case Java compiler looking for One as a package and then test.class., or to be very specific just look inside your .class file you can see what path it looking for. Please have a look for below Image (I my case I use Hello and Tester)
As you can see path in image is Hello/Tester(my case example), so architecture should be like Hello->Tester.
And if you are not having same architecture and want to create same while compiling, then use javacp command.