So I typed the very first example of Deitel&Deitel's How to Java book which is
public class Welcome1
{
// main method begins execution of Java application
public static void main( String[] args )
{
System.out.println( "Welcome to Java Programming!" );
} // end method main
} // end class Welcome1
Then I saved the file as test.java and went to its directory in cmd and typed Java test.java and I got the error Error: Could not find or load main class test.java.
What am I doing wrong?
You have two mistakes. Your class must match the file name. So move "test.java" to "Welcome1.java". Then you must compile it before you can run it.
javac -cp . Welcome1.java
Then
java -cp . Welcome1
First you have to compile the class with javac
javac Welcome1.java
Then you can call the class Welcome1
java -cp . Welcome1
The file name needs to be Welcome1.java. Class names and file names need to match.
Edit: And as others have mentioned you have to actually compile your code with the javac command before trying to run it with java.
Your class and Java file name must be the same. Like if you were to rename Welcome1 to test it would compile or rename the file to Welcome1.java it would compile.
Related
For example I have Main.java and test.java. test has public static int bro = 5; so i try to print test.bro from Main but the class test is not found. An IDE like Eclipse takes care of this for me but how do I do this with an editor? Sorry noob question. I'm in cmd in the directory of deez files and i type javac Main.java, den java Main. Thanks.
file Main.java:
public class Main {
public static void main(String[] args) {
System.out.println(test.bro);
}
}
file test.java:
public class test {
public static int bro = 5;
}
So suppose you have two source files: Main.java and test.java then you need to compile them first.
You can do it via command javac Main.java test.java. That command will produce 2 files in your current directory: Main.class and test.class. Which contain compiled java code.
Now you need to run your main class with classpath which contains both of your classes. So you need to run command java -cp . Main. Where . represents directory with your compiled classes.
I´d wanted to compile a simple Java "Hello World" program like it was repesented on the GeeksforGeeks Hello World Tutorial, by using gcj in Linux Ubuntu. This is the source code:
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, World");
}
}
But gcj threw two errors:
(.text+0x18): undefined reference to main
collect2: error: ld returned 1 exit status
Original output from the terminal:
gcj -o helloworld HelloWorld.java
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
I´d take attention on the requirement, that the .java file should be named after the class which holds main:
Important Points :
The name of the class defined by the program is HelloWorld, which is same as name of file(HelloWorld.java). This is not a coincidence. In Java, all codes must reside inside a class and there is at most one public class which contain main() method.
By convention, the name of the main class(class which contain main method) should match the name of the file that holds the program.
What am I doing wrong?
You are missing the --main= option, from the documentation, this option is used when linking to specify the name of the class whose main method should be invoked when the resulting executable is run.
gcj -o helloworld --main=HelloWorld HelloWorld.java
I am using spring framework.
I have an interface And a class In the same package.
My interface is
package soundsystem;
public interface CompactDisc{
void play();
}
My class is
package soundsystem;
import org.springframework.stereotype.*;
#Component
public class Sgtpeppers implements CompactDisc{
private String title = "A Movie";
private String artist = "The Movie is Being Played";
public void play(){
System.out.println("The CD is Played \n"+title+"\n"+artist);
}
}
On Compilation it gives me this error
Sgtpeppers.java:5: error: cannot find symbol
public class Sgtpeppers implements CompactDisc{
^
symbol: class CompactDisc
1 error
The Interface is Compiled First and the .class file is also stored the soundsystem package.
I Think there is something wrong with the javac Command.
The command i used is
javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java
Is this because i changed the classpath?
1) As a general rule, to compile classes, don't perform the task from the directory of a specific package.
Instead, use the root of the application source code as base to run the javac/java command.
By following this simple rule :
2)To compile all classes located in a same package, just specify the package as "source files" :
javac soundsystem/*.java
3)To compile a specific class depending on another compiled class of your source code (in the same package or not), you don't need to specify "." (that represents the current directory) in the classpath as it is the default value.
Java documentation states indeed that :
The default class path is the current directory. Setting the CLASSPATH
variable or using the -classpath command-line option overrides that
default, so if you want to include the current directory in the search
path, then you must include a dot (.) in the new settings.
But if you explicitly set the classpath with another value, the default value is not more used.
And here you did it :
javac -d . -cp "spring-framework-5.0.1.RELEASE/libs/*" Sgtpeppers.java
So you should add explicitly "." in the classpath too.
From the source code root, it would give on Windows :
javac -d . -cp ".;spring-framework-5.0.1.RELEASE/libs/*"
soundsystem/Sgtpeppers.java
For Unix, separator char is :, so it would give :
javac -d . -cp ".:spring-framework-5.0.1.RELEASE/libs/*"
soundsystem/Sgtpeppers.java
Running into an interesting problem
When I run:
$cd /projects/MyProject/
$java -cp . S3Sample
This works fine. However if I run:
$java -cp /projects/MyProject /projects/MyProject/S3Sample
Error: Could not find or load main class .projects.MyProject.S3Sample
Why is that. Did a quick look and can't find the answer. Thanks!
I have this folder structure:
- home
- org
- test
+ Foo.java
+ Foo.class
And the code in Foo.java is a simple hello world application:
//Note the usage of the package statement here.
package org.test;
public class Foo {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then, in command line, in order to execute Foo.class, I should provide the complete name of the class (I'm in "/home" folder in cmd):
$ java -cp "org/test;." Foo
Exception in thread "main" java.lang.NoClassDefFoundError: Foo (wrong name: org/test/Foo)
$ java -cp "org/test;." org.test.Foo
Hello world
Now, I edit the class above and remove the package sentence:
//no package specified
public class Foo {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
After recompiling the class and executing the same command lines:
$ java -cp "org/test;." Foo
Hello world
$ java -cp "org/test;." org.test.Foo
Exception in thread "main" java.lang.NoClassDefFoundError: org/test/Foo (wrong name: Foo)
TL;DR
Make sure to always specify the full name of the class. Check if your class belongs to a package. Specifying the path of the class to execute is the same as writing the full name of the class, java program will replace / by ..
You should run
$ java -cp /projects/MyProject S3Sample
The path for class is already CLASSPATH-relative
With java, you specify the fully qualified name of a class containing the main method you want executed. (The launcher will replace / with .). This class needs to be in the classpath. The argument is not a path to a file.
I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. Does anybody have any ideas concerning this problem? Thank you in advance
Here's a concrete example of what does work, so you can compare your own situation.
Take this code and put it anywhere, in a file called MainClass.java. (I've assumed a directory called src later. Normally you'd arrange the source to match the package, of course.)
package archiveFolder;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("I'm MainClass");
}
}
Then run each of these commands:
# Compile the source
javac -d . src/MainClass.java
# Build the jar file
jar cf archive.jar archiveFolder
# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows
# Execute the class
java -cp archive.jar achiveFolder.MainClass
The result:
I'm MainClass
How are you building your jar file? Is the code in the appropriate package?
Does theMainClassName class have the following package line at the top:
package archiveFolder
You need the class file to be in the same directory structure as the declared package. So if you had something like:
org/jc/tests/TestClass.class
its source file would have to look like this:
package org.jc.tests;
public class TestClass {
public static void main(String[] args) {
System.out.printf("This is a test class!\n");
}
}
Then you could use the following to create the jar file and run it from the command line (assuming the current directory is at the top level, just above org):
$ jar -cf testJar.jar org/jc/tests/*.class
$ java -cp testJar.jar org.jc.tests.TestClass
Perhaps with java -jar archive.jar?
Of course, it supposes the manifest points to the right class...
You should give the exact message you got, it might shed more light.
EDIT: See Working with Manifest Files: The Basics for information on setting the application entry point (Main class) in your jar manifest file.
Usually this happens when a dependent class (static member) is not found - like this, using log4j:
public class MyClass {
private static Logger log = Logger.getLogger("com.example");
}
The reason is that the initialization of such a static member can be understood as part of the class loading - errors causing the class not to be available (loadable), resulting in the error you described.
Static constructors are another possible reason:
public class MyClass {
static {
// <b>any</b> error caused here will cause the class to
// not be loaded. Demonstrating with stupid typecast.
Object o = new String();
Integer i = (Integer) o;
}
}
I think others have covered some common stuff here. I'd jar tf the jar and make sure the class is listed. I'd also double-check that the class is public and the method is "public static void main(String[] arg)".