New to Java , error already - java

package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
I keep getting this error when i run it
Error: Main method not found in class day1.examples.String2, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
and on line error it says type mysmatch cannot convert from java lang string etc..
Thank you

It works for me:
[steve#newbox ~]$ cd /tmp=
[steve#newbox tmp]$ mkdir -p day1/examples
[steve#newbox tmp]$ cat > day1/examples/String2.java
package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
[steve#newbox tmp]$ javac -classpath . day1/examples/String2.java
[steve#newbox tmp]$ java -classpath . day1.examples.String2
HelloAndrei Vlad
[steve#newbox tmp]$
The most likely explanation is that you have managed to get Eclipse rather confused.
My initial idea was that this was a homoglyph problem. But provided that you have copy-and-pasted the code correctly, the evidence disproves that.
The other idea I had was that you had mistakenly created your own version of the String class (in the day1.examples package). However, that should have resulted in compilation errors in the initialization of x.

I'd start by trying with a clean of the project. Seems weird you would need to do so with only one class in a single package but it will recompile all of your code for you. Everytime I have an issue that I don't think I should have I always F5 (refresh) my packages and then clean. You'd be surprised how many issues it fixes.
Go to Project --> Clean
Then try running again.

The code says that there are somethink wrong with your main class but all seems ok.
In Eclipse rigth click on your projects-> run-> run configurations and check if all is ok.

Related

Cant find or load main class

When I try to run a java program in the cmd it displays the messege "Could not find or load main class".
The problem occurs when there is a package involved in the program, otherwise it works fine.
The code is:
package myPackage;
public class index {
public static void main(String [] args){
System.out.println("Hello World");
}
}
I've tried writting in cmd: javac (name of package) . Class name, but still doesnt work.
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
Compile index.java from the top_level. do not use sub package.
$javac -cp . importpackage/subpackage/index.java

Error: Main method not found in class. can anybody will give me a proper solution for this error please

Error:
Error: Main method not found in class app, please define the main method as:
public static void main(String[] args)
Code:
class app
{
public static void main(String[] args) {
double accounts[];
accounts=new double[100];
accounts[2]=1225.33;
System.out.println("Account 2 is overdue by $"+accounts[2]);
}
}
I am using EditPlus to run and execute this program.
If you are trying to run a class you need the main method. Add the main method to the class and put the code you are trying to execute inside the main method and it should execute. Also post the code you do have so it's easier for us to understand what you are trying to do.
public static void main(String[] args) {
//code goes here
}
There is either something broken about the tools you are using, or the process you are using. (For example, you might have made a mistake with your classpath.)
At any rate, your program works for me, as demonstrated by the following:
[steve#newbox tmp]$ cat > app.java
class app
{
public static void main(String[] args) {
double accounts[];
accounts=new double[100];
accounts[2]=1225.33;
System.out.println("Account 2 is overdue by $"+accounts[2]);
}
}
[steve#newbox tmp]$ javac app.java
[steve#newbox tmp]$ java app
Account 2 is overdue by $1225.33
[steve#newbox tmp]$
For the record, this is with Java 8 tools ... and I haven't set the CLASS_PATH environment variable. (Hence, java and javac will just be using the current directory as the classpath.)
The only other possibility I can think of is that your original code has a nasty homoglyph in (maybe) the main identifier, that causes the java command to not see the method. There is a chance that StackOverflow will silently "fix" homographs in Questions and Answers. (It certainly seems to "fix" dodgy control codes.)

Issues with running a compiled java program

I am a Java beginner. I wrote a quintessential "Hello, World!" program. It compiles, but won't run. The terminal says there is an exception in the thread main, and that the class hello is not found. I am using Ubuntu 12.04. What could be wrong here?
The file is called hello.java.
The commands I used:
$javac hello.java
$java hello
My code is below:
class hello{
public static void main(String[] args) {
system.out.print("Hello");
}
}
EDIT-----------------------------------------------------------------------------------------
I just realized that I am using openjdk7. Does that pose a problem?
class must be: public class hello
system.out.print is wrong, must be: System.out.println("Hello World");
Is the filename hello or Hello? The only way I duplicated your problem was by having the class name wrong, and in java the class name an filename must exactly match (meaning the case too). So if your filename is Hello.java and the class name is hello the program will compile fine but throw the same error you mentioned. This is just a guess though.
Is that all your code? Since you use a terminal, can you add the results of the command ls -lR to your question? (run it in the same directory as your original command executing the application).

Error: Could not find or load main class Hello2

I'm just learning java and following a book.
I have a program written via text editor and run commands via cmd.
I've complied 1 program thru javac and executed thru java no problem. (Hello)
Then I modified that to add a comment to the class, named file Hello2.java. I compiled it with no problems, but upon execution, I receive this error: Could not find or load main class Hello2.
I have classpath and path set correct;y on environment variables.
Ideas?
UPDATE
Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Hello2.java
//Filename Hello2.java
//Written by
//Written on
public class Hello2 {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
/*This class demonstrates the use of the println() method to print the message Hello, world! */
I've found the solution to my problem. I know it's not a code problem. But what I did is that I deleted CLASSPATH from system variables and everything now works...at least for now.
Thanks a lot everyone for your inputs, much appreciated!
You have to change the name of the public class too when you change the name of the file. So, if your file is called Hello2.java, the class should be called Hello2 and not Hello.
Are you sure you set the classpath correctly? Why don't you try running java -cp the directory of the .class file Hello ? If that doesn't work please upload the full stacktrace.
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder then add . to your classpath.
Note that the windows classpath separator is a semi-colon ie ;
If your class file is saved in following directory with Hello2 program name
d:\sample
java -cp d:\sample Hello2
java -cp . Hello2
I believe you have Hello2.java file as below.
class Hello {
public static void main (String args[]) {
System.out.println("Hello");
}
}
Change it to
class Hello2 {
public static void main (String args[]) {
System.out.println("Hello");
}
}
The change is class Hello2 instead of class Hello.
Note : You should always have classname and file name SAME.
Good Luck!!!
Update 1
Are you doing below steps??
Wrote Hello.java
Compile by javac Hello.java
Run by java Hello
Rename Hello.java to Hello2.java
Rename class name i.e. class Hello to class Hello2
javac Hello2.java
java Hello2
I believe you are missing Step 6 & executing step 7 after step 5. Please confirm.

Java package - class not found, same directory

I have two Java classes "giveMyOb" and "dataConn" declared in the same directory. Both are public classes. "giveMyOb" has a static method "getMine()". Inside dataConn, I called the static method as
giveMyOb.getMine();
When I try to compile dataConn.java, the following error is returned.
"Cannot find symbol
symbol: variable giveMyOb
location : class dataConn
giveMyOb.getMine(); "
It used to work earlier. But is not working now. Why is that?
Additional Information: JDK 1.6. Windows 7. 64 bit.
Update(30 days after the question): When compiled from Eclipse, the classes are referenced and it works. But the same won't work when compiling from command line. I was unable to figure out the reason and nothing logical comes to my mind!
javac -classpath . *.java
ought to create both .class files at the same time. It's more complicated by packages. I'm assuming you have none.
Learn the Sun Java coding conventions. You aren't following them with those class names. They should start with a capital letter.
Try this:
giveMyOb.java
public class giveMyOb {
public static String getMine() {
return "Yay, it works!";
}
}
dataConn.java
public class dataConn {
public static void main(String[] args) {
System.out.println(giveMyOb.getMine());
}
}
Then compile it all:
javac *.java
and run the main class:
java -cp . dataConn
// output: Yay, it works!
Note that Java's coding conventions recommend class names start with a capital.
If "it" still doesn't work, try removing the .class files manually then recompile again.

Categories