I am fairly new java and programming as a whole. Currently i follow a guide to java programming but a few of the programs don't execute accordingly in eclipse mars but execute without problems on ideone.com. An example:
class Vehicle{
int Passengers, mpg, Fuelcap, Size;
boolean running, full, Fueltankempty;
void range(){
System.out.println("Range is " + Fuelcap*mpg);
}
}
class AddMeth{
public static void main (String args[]){
Vehicle minivan = new Vehicle();
Vehicle sportscar =new Vehicle();
minivan.Passengers=7;
minivan.Fuelcap=16;
minivan.mpg=21;
sportscar.Passengers=2;
sportscar.Fuelcap=14;
sportscar.mpg=12;
System.out.println("minivan can carry "+minivan.Passengers+" with a range of " );
minivan.range();
System.out.println("sportscar can carry "+sportscar.Passengers+" with a range of ");
sportscar.range();
}
}
When executing I get the following error message:
minivan can carry 7 with a range of
Exception in thread "main" java.lang.NoSuchMethodError: Vehicle.range()V
at AddMeth.main(AddMeth.java:26)
Does anybody know why I get the message?
You have to recompile your code, each time you made a change to your source code and want to run your program. If you are usingan IDE like Eclipse or Netbeans, it will (by default) automatically build/recompile.
In Eclipse, check, whether you have activated build automatically, which is in the Project menu.
Related
When I write a simple parametrised constructor program, it compiles and runs in the command line.
However when it is executed within the Eclipse IDE, I received the following exception:
Exception in thread "main" java.lang.NoSuchMethodError:
a_constructor.Test.(II)V at
a_constructor.ParametrizedConstructor.main(ParametrizedConstructor.java:15).
Code :
//write a java program which listed the concept of parameterized constructor
class Test {
int a,b;
Test (int x, int y) {
a=x;
b=y;
System.out.println("========================");
System.out.println("value of a=" +a);
System.out.println("value of b=" +b);
System.out.println("========================");
}
}
public class ParametrizedConstructor {
public static void main(String[] args) {
Test t1=new Test(10,20);
Test t2=new Test(100,200);
Test t3=new Test(1000,2000);
}
}
The ParametrizedConstructor code is clean and doesn't have any issues.
Try:
Delete the class files which were generated using the command prompt - If you are using the same location through eclipse to compile the same file.
Make sure the Java Compiler and Java Build path was matched with the JDK versions.
Alternate Solutions:
Try placing the code in Eclipse > Java Project > Under default package and run the file.
We need to have the make sure that the compilation unit is matched to the class name ParametrizedConstructor.java (i.e.., public class)
For References - Check also the below links for better understanding:
Possible Causes of 'java.lang.NoSuchMethodError: main Exception in thread "main"'
public class AssignClass {
public static void main(String[] args) {
int numArr[] = {82,60,72,50,3,39,47,20}; //integer array
int smallTemp = numArr[0]; //assigning zeroth element as small number
int largeTemp = numArr[1]; //assigning first element as large number
for(int i=0; i<numArr.length;i++) //iterating till the end of the array
{
if(largeTemp<numArr[i]) //check if i'th value of array is large than largeTemp
{
largeTemp=numArr[i];
}
if(smallTemp>numArr[i]) //check if i'th value of array is small than largeTemp
{
smallTemp=numArr[i];
}
}
System.out.printf("Largest Number: %d\n", largeTemp); //print large number
System.out.printf("Smallest Number: %d\n", smallTemp); //print small number
}
}
The printf is underlined in red and gives and error? What is wrong? I used Eclipse
printf is available since Java 5., so looks like your project is configured to use Java 1.4. Change the configuration of your project by right clicking on it, then go to Properties:
Select Java Build Path option and make sure you're using the proper Java JDK version for your project.
Select Java Compiler option and make sure you're using Java 1.5 or superior.
If you happen to create a project in Java 8, make sure you're using Eclipse Luna or Eclipse Kepler with the plugin for Java 8 support. IMO I recommend you to use Eclipse Luna. DISCLAIMER: I'm not contributor of Eclipse or any other Eclipse-base technology, just a happy user of this tool.
I'm using Eclipse for Java on my Macbook air, OS 10.9. I keep getting the error Obsolete Methods on the Stack, with a warning about how it may cause problems with the virtual machine when I run very basic programs that have no errors. I ran a program that just had the class and the main method and I got this error. After the error box, the bug referred to the main method, but I know the syntax is correct because I used Eclipse's main method.
import java.util.Scanner;
public class Dowhile {
public static void main(String[] args) {
/*Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number");
int value = scanner.nextInt();
System.out.println(value);*/
/*do{
System.out.println("Enter a number ");
int value = scanner.nextInt();
}
while(value != 5);
System.out.println("Got 5");*/
}
}
Update:
I'm not getting the obsolete methods error now, just Exception in thread "main"... at line 5.
This error message indicates that you are doing hot code replace, and that
the frames on the stack no longer match the class files in the running VM.
Restarting your debug session/target VM should suffice.
Hot code replace (HCR) meaning:
It is a debugging technique whereby the Eclipse Java debugger transmits new class files over the debugging channel to another JVM.
read more.
Bugzilla
Dreamincode
I clicked on Terminate button and it no longer showed that warning message again.
I know that Eclipse has powerful debugging capabilities. Are there any hooks that allow plug-ins to keep track of the code path followed when an open project is run?
For example, suppose I had the following program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = 0;
if(in.nextInt() == 1) {
num += 2;
} else {
num += 3;
}
System.out.println(num);
}
}
Is there some API that Eclipse exposes that would let me make a plug-in that determines which branch of the if statement this program took after it's executed once?
You might be interested in looking into code coverage. It's a metric used to determine what parts of your code have been run after execution. Typically, it's used for testing purposes, to see what parts of your code aren't run.
That said, you could use it to determine what branch was chosen by only running the program once. EclEmma, an eclipse plugin, will show you what branches were used by coloring the lines on the editor itself.
Here is my code
import java.util.Scanner;
public class Range {
public static void main(String[] args)
{
System.out.println("Greetings.");
int min,max;
System.out.println("Enter a minimum and maximum value.");
Scanner keyboard = new Scanner(System.in);
min = keyboard.nextInt();
max = keyboard.nextInt();
System.out.println("The number of values in the range from " + min + "to " + max + " is");
for (int i = min; i <= max; i++){
System.out.println(i);
}
}
}
Exception:
run:
Error: Could not find or load main class project.Project
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
could someone tell me what I am doing wrong?
That's not a compiler error - that's when you're trying to run the code.
And the answer is simple - you're trying to run project.Project, but your class is actually just Range (in the default package, by the looks of it).
You are told that Java Runtime can't find the main class to run the app, and it looks like it's set to project.Project whereas it should be set to Range since that's the full name of the class containing your main method.
Right click the project, select Properties, then go to the Run tab. Then set Main Class to Range. Assuming you're launching the correct project, you'll be fine. If not, then check if it is set as main project (right click it and select the corresponding menu item) or right click it and select Run to launch it.
Set your main project. This is not a compile error.
You need to learn using NetBeans, the different steps to write, compile and run the programs. Once you know all this such errors will never bug you. Here is the tutorial to help you with that:
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/netbeans.html