GCJ throws error: "Undefined reference to main" when compiling - java

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

Related

java11 allows the compilation and execution in a single line

in a file named filename.java
class filename{
public static void main(String[] a){
System.out.println("From filename main method");
}
}
public class ClassName{
public static void main(String[] a){
System.out.println("From First main method");
}
}
Observe below commands:
Command 1:
C:\javaDJ>java filename.java
From filename main method
Command 2:
C:\javaDJ>javac filename.java
filename.java:7: error: class ClassName is public, should be declared in a file named ClassName.java
public class ClassName{
^
1 error
Observation:
command 1 compiles (i assume internally ) and executes successfully.
command 2 throws compilation error.
Problem Statement :
How is java cmd able to compile the file called filename.java, when the file(filename.java) contains a public class (ClassName)which is not named 'filename.java' (the name of the file-name.) ?
To highlight a specific section from the JEP#Launch Single-File Source-Code Programs with regards to the behavior
In source-file mode, execution proceeds as follows:
The class to be executed is the first top-level class found in the
source file. It must contain a declaration of the standard public
static void main(String[]) method.
The feature which enabled you to execute Command 1 successfully was introduced in Java 11. The feature allows you to execute a Java source code file directly using the java interpreter. The source code is compiled in memory and then executed by the interpreter, without producing a .class file on disk. Check this for more information.
The error you got in Command 2 has been there since the beginning of Java.

Error while compiling a simple Java code

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.

Unsure about reason behind NoClassDefFoundError

I have built a DLL which I am attempting to wrap Java code with, however I am having some troubles with running my Java program. I wrote a simple test DLL and Java program and am producing the same error, and although there are plenty of resources regarding NoClassDefFoundError online I can't seem to solve mine with any troubleshooting methods.
Here is my D:\Test1.Java file
public class Test1 {
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
public native void displayHeyLand();
public static void main (String[] args) {
Test1 t = new Test1();
t.displayHeyLand();
}
}
After compiling, attempting to run D:\Test1.classresults in the following:
D:\>java Test1.class
Exception in thread "main" java.lang.NoClassDefFoundError: Test1.class
Caused by: java.lang.ClassNotFoundException: Test1.class
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:660)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:626)
Could not find the main class: Test1.class. Program will exit.
Why I am stumped :
1. I have set my classpath to be D:\, so I believe my class definition would be in the classpath, and I do not see how my compile-time and run-time classpaths could be any different.
2. I don't see how this could have anything to do with static initialization, and I believe the exception would look different.
Perhaps I'm just missing something incredibly simple, I am very newbie with Java.
Any help is greatly appreciated!
The classpath environmental variable is taking precedence over that in the java run command. You need to specify the class location (as well as removing the .class file extension)
java -cp . Test1
Java normal syntax for executing class file is
Java [<options>....} <class-name> [<arguments>....]
For example
java com.package.name.Test1
here how compiler works
1. Compiler search for complete class name
2. Load that class
3. check for main method - in the same class
4. Call main method with passed arguments in command line string.
Now following are the possibilities why your class may not found main method.
1 - forgot to include package name
I am new developer in java but I found when I run application using eclips or intellJ editor it gives different path and package name and execute code as I noticed it on command line edior. So make sure you are including package name
For example:
java com.package.name.Test1 instead of
java Test1
2. File name or pathname rather then class name
As I noticed output file is in different location. That why class file path was different.
java Test1.class
java com/package/name/Test1.class
3. Typo
also I noticed you are using
static {
//System.loadLibrary("HeyLand");
System.load("D://HeyLand.dll");
}
Is this function ? or constructor? If it is function then where is name of the function? You cant write code without any reference in classs

java relative path vs absolute path on command line

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.

'Exception in thread "main" java.lang.NoClassDefFoundError' when running java program from command line

What am I doing wrong here:
class Helo {
// main: generate some simple output
public static void main (String[] args) {
System.out.println ("Hello, world."); // print one line
System.out.println ("How are you?"); // print another
}
}
When I go into terminal I do:
cd ~
javac Atempt2.java (//that's the file name)
java Atempt2
and then it gives me this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
So all in all this is what I do and what happens:
david-allenders-macbook-pro:~ davidallender$ cd ~
david-allenders-macbook-pro:~ davidallender$ javac Atempt2.java
david-allenders-macbook-pro:~ davidallender$ java Atempt2
Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
david-allenders-macbook-pro:~ davidallender$
I am very new at this so please explain things in a very simple manner.
Thanks.
Its been awhile since I've done any java work but I'm pretty sure your class name needs to match your file name.
javac uses the class name to generate the output not the filename.
So it will generate a Helo.class classfile.
java will take a class name and call the main function in the corresponding class file, here Hello.class.
The ClassNotFoundError is thrown because javac never generated an Atemp2 classfile as there is no Atemp2 class in your source file.
Rename your Atempt2.java to Hello.java to get going, then:
javac Helo.java
java Helo
See here for more discussion and the reasoning.
change:
class Helo
to
class Atempt2
in your source file.
A .java file that declares a class must have the file name match the declared class name.
The filename must match the name of the public class defined in the file. In this case, you would either have to name the file "Helo.java" or renamed the class to Atempt2.
This is the very basic to start with java programming.Any program you write the name of the file must match with the public class of the program.
Here in your program public class of the file is Helo so your file name must be Helo.java.Here the compiler is able to compile but JVM will search for Helo.class file to run. As there is no Helo.class file you are getting runtime Exception Exception in thread "main" java.lang.NoClassDefFoundError: Atempt2
to complement josefx's answer.
The argument to the compiler (javac) is the name of the file or files to compile (as you did).
On the other side, the virtual machine (java) gets the name of the class whose main method is to be executed.
One option would be
javac Atempt2.java // the file name
java Helo // the class name
Normally it is a good idea to have the file named the same way as the class. For public class this is a must (checked by compiler).

Categories

Resources