I'm just starting Java ... again.
I just made a simple program
class first
{
public static void main()
{
System.out.println("Hello!");
}
}
This runs perfectly fine in BlueJ but it gives an error during run-time when running from command prompt.
This is the error
Exception in thread "main" java.lang.NoSuchMethodError: main
It's because I didn't give String args[] in the main parameter list
Till now, I used to give it subconsciously. I know that the string array contains all the parameter values when running but then why is it running in BlueJ?
(BlueJ is a student-friendly Java editor and compiler)
Your program is valid and will compile to the same thing whether you compile from BlueJ or from the command line.
However, blueJ will let you run any static method in a class (so you can test your functions) where as the command line java command will (only) look for a special main method to run. This main method tages a String array with all the command line parameters and your program should look like this even though you don't use these command line parameters:
class first
{
public static void main(String[] args)
{
System.out.println("Hello!");
}
}
Related
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.
I finished creating a program but I was told that my program
must be a Java application that takes as a command line argument the name of the file."
I understand I can use the jar command in terminal but I don't undestand how you open the terminal and take a file name as a argument. I was wondering if someone could explain what code is required to do this.
Thanks alot.
I tried creating a basic jar file in terminal with the line "jar cvf findOptimalTransport.jar ." but the jar file does not open, I think its because the current implementation takes the users input with a scannar in the code and prints via the terminal. However, this wont work because a terminal window is not opened with this command.
It doesn't have to be a jar file. Command line arguments can be entered from the command line, when you run your application.
Let me give you an example, about how this works. Let's say you have the below simple Java application:
public class MyApplication{
public static void main(String[] arguments){
System.out.println("Hello World!");
}
}
That public static void main() is a method; and more specifically the main method of your application which is what is executed when compiled and ran.
To compile and then run it, you type in the command line/terminal:
javac MyApplication.java //this will compile it
java MyApplication //this will run the main method of MyApplication
But what is that parameter in the main method? What is String[] arguments ?
When you run your program, whatever you type after the application name is an argument, of type String and it is stored in the String array String[] arguments (or most commonly String[] args).
What this means, is that, if you execute your application like this:
java MyApplication some_file.txt // Run application with one arg.
You can access that argument like so:
public class MyApplication{
public static void main(String[] arguments){
System.out.println("Hello World!");
System.out.println("You entered: " + arguments[0]);
}
}
Output:
Hello World!
You entered: some_file.txt
Note: To run a jar file, you need to navigate to the folder that the jar file is in and from the command line you can run it by typing:
java -jar <jarname>.jar
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
Here an alternative is to write your own launcher, this way you can define entry points as you want.
In the article JVM Launcher you will find the necessary information to get started:
This article explains how can we create a Java Virtual Machine
Launcher (like java.exe or javaw.exe). It explores how the Java
Virtual Machine launches a Java application. It gives you more ideas
on the JDK or JRE you are using. This launcher is very useful in
Cygwin (Linux emulator) with Java Native Interface. This article
assumes a basic understanding of JNI.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static initializer block; details here.
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
Applets from what I remember do not need a main method, though I am not sure they are technically a program.
I was going through some basic java concepts. I was looking into methods and trying to verify this error
"MethodSignature.java:10: error: method m1(String) is already defined in class MethodSignature
public static void m1(String s)"
The above error appears when I run from command prompt. But when running through eclipse, although it shows error, program prints the desired strings. I do not get any error as stated above in case of command prompt.
Why there is a difference in execution in Eclipse and command prompt?
As in command prompt, I am not able to run the program itself because the error should stop me. I was expecting the same in Eclipse.
Here is my simple program.
public static void m1(String s)
{
System.out.println(s);
}
public static void m1(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
m1("call one");
m1("call two");
}
You must be running a previously compiled class, you cannot declare m1 twice. However, eclipse does have its' own compiler (ecj); and it is possible to run code ignoring errors (in which case it removes the invalid code for you).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
Here an alternative is to write your own launcher, this way you can define entry points as you want.
In the article JVM Launcher you will find the necessary information to get started:
This article explains how can we create a Java Virtual Machine
Launcher (like java.exe or javaw.exe). It explores how the Java
Virtual Machine launches a Java application. It gives you more ideas
on the JDK or JRE you are using. This launcher is very useful in
Cygwin (Linux emulator) with Java Native Interface. This article
assumes a basic understanding of JNI.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static initializer block; details here.
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
Applets from what I remember do not need a main method, though I am not sure they are technically a program.