package pack;
public class sample{
public static void main(String input[])
{
NumberFormat numberFormat = new DecimalFormat("#,##0.00##");
System.out.println(numberFormat.format(44533125.00));
}
}
the code is working fine in the current dir.. (c:/myprogram/).
after that i copy the sample.class file and paste it in other dir(d:/myprogram).
i got error while running, like
Exception in thread "main" java.lang.NoClassDefFoundError: sample (wrong name: pack/sample)
In java .class file can run anywhere right? but why i am not able to run?
You should have the class file within the package - so it should be in a directory called pack. Then with the parent directory in the classpath, you'd run
java pack.sample
(You should also change the class name to Sample to follow conventions, btw - and run pack.Sample.)
If you're building with javac, specify the "-d" option to tell it the base directory, and it will create the appropriate package structure if necessary. For example:
javac -d classes Sample.java
or
javac -d classes src/pack/Sample.java
will (in both cases) create
classes/pack/Sample.class
You could then run
java -cp classes pack.Sample
IntelliJ and maybe other IDE's do not refactor your Run/Debug configuration. You must manually change your package name preceding the name of your Main class. For instance, change 'sample.Main' to 'com.company.package.ui.Main' so it will launch correctly next time you try to run it.
The IDE might have already marked the Run/Debug button with a red cross because it couldn't find the main class. It also gives a warning when you open the Run/Debug configuration.
If you are not using a single java/class file you can also remove the package statement.
Related
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.
I'm just getting started with Java. This is my program:
package javaapplication1;
public class JavaApplication1
{
public static void main(String[] args)
{
System.out.println("Hello ");
}
}
If I remove the first line package javaapplication1, the code won't run. I did the same thing in class but it was working.
Can someone explain why does this happen?
If you are working with terminal along with package statement, save your code as JavaApplication1.java
and then compile your current code with below syntax.
javac -d . JavaApplication1.java
( -d . indicates create directory in current location because we are using package statement).
then for execution of your code you need to change directory with
cd javaapplication1
then execute your code with
java JavaApplication1
It will execute fine.
But if you are working without package statement seen will differ, you need to compile code normally with
javac JavaApplication1.java
then execute code with
java JavaApplication1
You will not get an error.
Note: But If you are using any IDE nothing to worry about. IDE will take care of package statement.
error: Could not find or load main class: this error appears at runtime if JVM is unable to find main class.
You need to change the directory as I mentioned above, then it might work fine.
This has to do with the way your IDE sets up your project.
When you start learning a new language, it's always a good idea to skip the IDE until it actually becomes useful. I suggest you copy your program to a basic text editor, remove the package line, save it as JavaApplication1.java and manually compile and run with javac JavaApplication1.java and java JavaApplication1.
In Eclipse, I wrote a Java class Test with a main() function.
The project in which is defined the class, I added the jar file bcprov-jdk15on-151.jar (I am using the library BouncyCastle).
In Eclipse, there is no problem and my program runs normally. But when I try to do it in a terminal, I get an exception.
After checking SO I found a similar post: NoClassDefFoundError while running a java program in terminal from IDE but the solution given doesn't work.
To illustrate my case, in the directory C:\Docs\workspace\Terminal\bin\ I have the file Test.class. If I run java Test I get Exception in thread "main" java.lang.NoClassDefFoundError: org.bouncycastle.math.ec.ECFieldElement.
If I run java -cp bcprov-jdk15on-151.jar Test (I put the .jar in the same directory to simplify) I get Error: Could not find or load main class Test so it seems that the dependency error is solved but another one occurs.
What am I doing wrong? Just to give the structure of my .java file:
import java.io.*;
...
public class Test {
... local methods ...
public static void main(String[] args) {
...
}
}
Thanks in advance.
Try this, you forgot to include current path "."
java -cp ".;bcprov-jdk15on-151.jar" Test
Hope it help
I wrote and tested a small Java program using Eclipse. I'm now trying to deploy it on a Windows 7 box and Java cannot find the class. I copied the class file to C:\dxtester\classes. I'm trying to run it from the dxtester directory with: C:\dxtester>java -classpath classes;. dxtester
This produces this exception which I think I understand. Java examined the class file and is prompting me to provide the fully qualified name.
Exception in thread "main" java.lang.NoClassDefFoundError: dxtester (wrong name:
dxtester/dxtester)
If I use the FQN I get
C:\dxtester>java -classpath classes;. dxtester.dxtester
Error: Could not find or load main class dxtester.dxtester
The application is a simple test driver where everything is done in main().
package dxtester;
public class dxtester {
public static void main(String[] args) {
This seemed like an extremely simple thing to do but I'm completely baffled. What am I missing?
Your current directory is dxtester;
in this directory you have dxtester.class (I presume);
your classpath is the current directory.
This setup is wrong: your classpath must be the base directory such that package names correspond to its subdirectories. In your case you should cd to C:\ and repeat the command; ideally, however, you will have your package structure in a dedicated directory instead of the root.
I should also mention that class names should be in CamelCase.
I am using the code from Rome's tutorials page http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader . Also trying this one: http://wiki.java.net/twiki/bin/view/Javawsxml/Rome05TutorialFeedReader
Compiling works, but I'm not sure how to run these examples. Why I just type java FeedReader or java FeedAggregator into the command line, I get the error:
C:\projects\freshmeat\src>java FeedAggregator http://freecode.com/?format=atom
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/syndication/f
eed/synd/SyndFeed
plus the large block that follows this error
Why is this happening, how do I fix it and try these things out? How do I get something to work with Rome!?
You need to include rome in the runtime classpath (in addition to the compile-time classpath)
java -classpath lib/rome.jar FeedAggregator ...
The samples you are trying to run are in the package com.sun.syndication.samples. You say you are a complete beginner, so, to make things simpler, I would recommend that you remove the line beginning with package in each of FeedReader.java and FeedAggregator.java. Recompile the classes after removing their package directives.
Then, to run these classes, make sure you're in the same directory as the class files FeedReader.class and FeedAggregator.class that javac created. Then, try running:
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. FeedReader
(and similarly for FeedAggregator.)
Note also that I've added the current directory, ., to the -cp attribute. Without this, the Java virtual machine won't know that it has to look in the current directory to find your FeedReader and FeedAggregator classes.
If you were to reinstate the package directives, you'd find the class files FeedReader.class and FeedAggregator.class would be created inside a directory com\sun\syndication\samples when you compile their sources. To run the class files from this location, you'd use a command line such as
java -cp c:\projects\freshmeat\libs\rome-1.0.jar;c:\projects\freshmeat\libs\jdom-1.0.jar;. com.sun.syndication.samples.FeedReader
and you'd run this from the directory containing the com subdirectory, not the directory that contains the class files.
More information on packages in Java can be found here.