Eclipse is refusing to compile my java files when there are more than one main methods in the same package. As far as I know, this should work, but it compiles once and then never updates the .class files in the bin folder. Is there any way to force Eclipse to compile the class files?
Java allows only one method as main method which have the following syntax.
public static void main(String[] args) {
}
or
static public void main(String[] args) {
}
If you will try to make the duplicate main method then it won't allow because the JVM becomes confused to make decision while calling the main method.
But here you can try the methods like.
public static void main(int[] args) {
}
or
public void main(String[] args) {
}
or any methods which doesn't have the exact method as
public static void main(String[] args) {
}
Related
i am bit new in java what i want to achieve is, I have 2 java classes in same package.
CodeAnalyzer.java and CodeReader.java
CodeReader is the Jform class in which i have made JFileChooser, now i want to execute CodeReader with the execution of CodeAnalyzer.
Source code of CodeAnalyzer is Below
public class CodeAnalyzer {
public static void main(String[] args) {
// TODO code application logic here
}
}
public class CodeAnalyzer {
CodeReader cr;
public static void main(String[] args) {
cr = new CodeReader();
}
}
Just make an Object of your CodeReader and if it executes a Code which promts a Window it'll do it
basically i want to create a simple main class in Eclipse EE like this
package com.baileyproject;
public class Add {
public static int addNumber(int n)
{
n += 1;
return n;
}
public static void main(String[] args)
{
System.out.println(addNumber(5));
}
}
Then it would show the error like:
Error: Main method not found in class com.baileyproject.Add, please
define the main method as: public static void main(String[] args)
or a JavaFX application class must extend
javafx.application.Application
It just happened with Eclipse EE (for Web Developer) only, if anyone knows how to fix this, i'd be very grateful.
I created a dynamic web project and added your code to it. Everything worked out well. check if your project settings are correct.
//: innerclasses/TestBed.java
// Putting test code in a nested class.
// {main: TestBed$Tester}
public class TestBed {
public void f() { System.out.println("f()"); }
public static class Tester {
public static void main(String[] args) {
TestBed t = new TestBed();
t.f();
}
}
} /* Output:
f()
*///:~
I am studying "Think in Java". I am just wondering why the above code doesn't work which should be a way to test each class, and can be removed by deleting TestBed$Tester.class file.
The error msg instructs there should be a public static void main(String[] args) in TestBed class as program entry.
java compile version: javac 1.7.0_40
The main method must be in the public top-level class. That is the one with the same name as the java-file. Here, that's the TestBed-class.
The current main method is in an inner class (namely TestBed$Tester), and can't be used to start a program.
EDIT: I may have been wrong. I took a look in the book you mentioned, and it looks like you're able to run the inner class from the Command Promt by writing:
java TestBed$Tester
I started learning Java lately, and trying to make my first program based on book which is the "Hello World" program.
After writing the script on notepad, i try to compile it on the command prompt and then this notice appeared.
first I type: javac javaCode.java
then there a notice said:
javaCode.java:2: error: <identifier> expected
public class static void main(string[] args) {
^
javaCode.java:6: error: reached end of file while parsing
}
^
2 errors
I don't have any ideas what going on here so please give detailed information and how to fix this thing.
public class static void main(string[] args)
Remove class
public static void main(string[] args)
class is different from method. main method syntax doesn't contain class.
Your main method needs to go inside a class: Java won't let you do things any other way.
Try:
public class HelloWorld{
public static void main (String[] args){
//your code goes here
}
}
this the basic structure for a simple program like this.
the class identifier, has to be on the declaration of the class.
public class Caculator {
public static void main(String[] args) {
// Your code here
}
}
It probably should be:
public class JavaCode {
public static void main(String[]args){
}
}
That should do it. ;)
I was trying to know what would happen if I have two classes with the main function.
I used the following code:
class A {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
class Hello {
public static void main(String[] args){
System.out.println("Hello,World!");
}
}
I compiled it using javac First.java (since no class is specified as public , I named the file as First.java); it got compiled without any error and i ran only the class A.Expecting the Hello class to run itself. DIDN'T HAPPEN(?),maybe the program ran out of scope.
So,
I tried compiling the following java code(I am a beginner) but i got the following error.
Code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello,World!");
}
}
class A {
public static void main(String[] args) {
System.out.println("Hello,World!");
Hello.main();
}
}
I compiled it through javac First.javaand got the following error:
method main in class Hello cannot be applied to given types;
Hello.main();
^
I wanted the program to run first the class A's main function and then class Hello's.
What's going wrong here?
Look at the declaration of Hello.main:
public static void main(String[] args)
Now you're trying to call it like this:
Hello.main();
What would you expect the value of args to be, within the method? You need to provide it with a value for args... and fortunately, you already have one, as you're within a method which uses args as a parameter, also of type String[]. So you should just be able to change your code to:
Hello.main(args);
Note that the two args parameters - one for Hello.main and one for A.main are entirely separate. We happen to use pass the value of one to the provide the initial value for the other, but we could easily have written:
Hello.main(null);
or
Hello.main(new String[] { "Some", "other", "strings" });
instead.
Do this in your second class:
public static void main(String[] args) throws IOException {
MyOtherClass.main(args);
}