unable to find main in NetBeans - java

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

Related

How to solve "Build Failed" error in Java Code runner in VS Code

package Java.School;
public class Compound_Interest {
public static void main(String[] args){
double principal = 100;
System.out.println("The principal is " + principal);
double rate = 8;
System.out.println("The rate of interest p.a. is " + rate + "%");
double time = 2;
System.out.println("The time for which the principal has been deposited is " + time + " years");
double CI = principal*Math.pow(1 + rate/100, time);
System.out.println("The compound interest received is " + CI);
}
}
So this is my code. But when I am running it, I am getting an error as "Build failed".
On clicking on fix, I get those options of clearing caches and all but they don't work.
On clicking on Proceed, I get another error -
"Error: Could not find or load main class Java.School.Compound_Interest
Caused by: java.lang.ClassNotFoundException: Java.School.Compound_Interest"
The problem is the way that you try to execute this program.
The compiler is trying to find the path-package Java/School and inside it the class Compound_Interest.
So if you want to run only the file you could remove package Java.School; and then run it with:
javac Compound_Interest.java
java Compound_Interest
Also while we are on this topic you should check out how to run java as a project.
I hope I helped.
My guess is that your package being camel case is the problem. Java.School vs java.school
I also think this is OS dependent since Java != java in some OSes (Linux), but is OK in others (Windows). It wouldn't surprise me if the IDE is creating java/school as the directory which works until the IDE reboots and/or the OS reboots.
Try changing the package name to all lower case.

Java- Eclipse error

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.

How do i stop System.out.printf to stop giving me error in java eclipse [duplicate]

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 compile and then run this bit of code in java.exe and i receive an error

I can't seem to figure out what is wrong with this code. Eclipse tells me main method isn't declared. and when I run it in java.exe it tells me "could not find or load main class discount.java" I've spent the last half hour looking for a solution but can't seem to figure it out.
import java.util.Scanner;
public class Discount
{
public static void main (String[] args)
{
Scanner scan = new Scanner( System.in );
int price;
System.out.println("Enter the Price:");
price = scan.nextInt();
System.out.println( price / 4 * 3 );
}
}
The commands I'm using and error I'm getting:
> CD C:\Programing\Misc
> set path=%path%;C:\Program Files\Java\jdk1.8.0\bin
> javac discount.java
> java discount.java
Error: Could not find out or load main class java.discount
Are you using java discount.java? That's likely the issue.
Try these two lines:
javac discount.java
java discount
That should run your main method (assuming that you've correctly named the file discount.java).
--
EDIT: After seeing your comment about changing the class name, you'll want to rename the file to Discount.java. Then run javac Discount.java and java Discount
The filename has to match the classname exactly, so put this in the file Discount.java (discount.java won't work).
Then, from my command-line:
% javac Discount.java
% java Discount
Enter the Price:
^C
Have a look at this file hierarchy and how to compile java on command line

Use Eclipse to Programmatically Track Execution Path

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.

Categories

Resources