Unable to run program - java

I've been using Eclipse for running Java Programs. Everything was going well earlier but now I'm unable to get the option "1 Java Application" when I click on "Run as" despite having zero errors in my program. Could anyone help me how to deal with it ?
class Base{
public int baseVar;
public int var;
public Base(int v){
baseVar=v;
System.out.println("Base class parameterized constructor");
}
}
class Der extends Base{
public int derVar;
public int var;
public Der(int v){
super(v);
derVar=v;
System.out.println("Derived class parameterized constructor");
}
public void display(){
System.out.println("Base variable value="+baseVar);
System.out.println("Derived variable value="+derVar);
}
public void useOfSuper(){
var=15;
var=20;
System.out.println("Base variable var=" +
super.var);
System.out.println("Derived variable var="+var);
}
}
class abc{
public static void main(String args[]){
Der Derobj=new Der(10);
Derobj.display();
Derobj.useOfSuper();
}
}

I suspect your program doesn't have an appropriate main method:
public static void main(String[] args)
If it does, try running it from the command line - does that work?
EDIT: As noted in comments, whether or not the presence of a main method affects the context menu appears to depend on the version of Eclipse. In the version I'm using at home (4.2.1) the context menu option doesn't appear unless there's a main method.

Change :
class abc
with
public class abc
I suspect that the class is private, hence you can't run it. You have to change your java name file to abc.java and make the class abc public.

Please use below. Your code is missing public identifier for class that contains main method.
public class abc {
public static void main(String args[]) {
Der Derobj = new Der(10);
Derobj.display();
Derobj.useOfSuper();
}

Your main() is in your class abc, move it into your Base and things will work (I am assuming here that all of the code you published has been placed in a single file named Base.java)

Related

Cannot find symbol using System.out.println(""); (NetBeans, IntelliJ or Eclipse)

Bad english alert
Whenever I try to use System.out.println on another class besides main, every single IDE installed in my PC returns the error on the title.
I'm writing a really simple code.
On IntelliJ, I had already tried to use "Invalidated caches" but didn't work as well.
Works here:
package javaapplication3;
public class JavaApplication3 {
public static void main(String[] args) {
System.out.println("Hi"); /* <-- This works on main, but doesn't
work in any other class opened in
another tab*/
}
But not in this other tab:
package javaapplication3;
public class NewClass {
System.out.println("Hi");
}
UPDATE
Here some images to specify the problem: Work here, but not here.
All code has to be in methods.
The command System.out.println("") will work only in a method.
Placing it under a class but not a method will result in the compiler throwing an error.
Eg:
public class test { // class
public static void main(String args[]) {
// inside main method
System.out.println("Hello, World!"); // correct
}
}
will work perfectly fine.
But, if you place the command under just a class, it's going to result in an error.[Needs to be in a certain method]
Eg:
public class test{
// inside a class, but no method
System.out.println("Hello, World!"); //incorrect
}
Also, you need to make sure your class and function are not reserved keywords.
And from what you've specified above, main in not a class but it's the main method of the class.

nested class as testing method in Java

//: 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

main method failed to execute glitch?

Was just testing a simple code, and it appears my eclipse just got worse. This code is suppose to output 2. But when I run it, very weird error says 'Error: Main method not found in class jasc1, please define the main method as: public static void main(String[] args)' when my main method is clearly defined.
Does anyone know what this error is all about??
public class jasc1 {
int a = 2;
public void abc(){
System.out.print(a);
}
public static void main(String[] args){
new jasc1().abc();
}
}
This works fine for me, your file name must be wrong. It MUST be the same as the class name.
Additionally
Class names should (by convention) begin with an upper case letter so Jasc1
If you want to execute a class It should have a method public static void main(String [] args) (or similar meanings). But, for executing a class you have to run that class.
ex: In command line you call java jasc1 after compiling it with javac jasc1.java (ofcourse there are some options like -cs; see help)
The same way you can run one class in Eclipse or NetBeans IDEs by right-clicking on it in the project explorer and select Run or Run as
Of course, this jasc1 class cannot call another class that have a public static void main(String []args) method.

How to run a package from another package inside the same project?

I have two packages inside my project, a and b.
a is my "main class" that runs when the program runs but I need to make b run from a (if that makes sense).
I'm sure its something along the lines of PackageB.BMain but I'm not sure.
Edit:
Okay so I've learned a few new things, to start my main project is RevIRC, inside that I have two packages, MainChat and RevIRC, now when I run the program RevIRC is ran, I need to make Mainchat run when RevIRC is ran.
Like I said before I'm sure its something along the lines of RevIRC.MainChat.ChatMain() but I can't seem to figure it out.
You have 2 options:
Either create a new instance of B from A, like so: PackageB.BMain b = new PackageB.BMain();
Access the methods in BMain in a static way like so: PackageB.BMain.someMethod();`
Note that you can use either of these exclusively or mix them up together, however, it all depends on how you have written your BMain class.
So for instance:
package PackageB
public class BMain
{
public BMain()
{ }
public void foo()
{
System.out.println("This is not a static method. It requires a new instance of BMain to be created for it to be called");
}
public static void bar()
{
System.out.println("This is a static method. It can be accessed directly without the need of creating an instance of BMain");
}
}
Then in your main class (the class which has the main method):
package PackageA
public class AMain
{
public static void main(String[] args)
{
PackageB.BMain.bar();
PackageB.BMain bInstance = new PackageB.BMain();
bInstance.foo();
}
}
If you have two main methods it will either run from A or B. The JVM will choose the first main method it sees IIRC.
Have a standalone class that will have main. And create your classes there.. ?
import a.Class1;
import b.Class2;
public class MainController
{
public static void main(String args[])
{
Class1 class1 = new Class1() ;
Class2 class2 = new Class2() ;
//Both class no start at the "same" time.
}
}
if i am not wrong you want to run main method of class B from class A.
That you can call using B.main(arg[]);
eg :
package a;
public class A
{
public static void main(String[] args)
{
System.out.println("This is main method of class A");
B.main(null);
/*pass any args if you want or simply set null arg*/
}
}
package b;
public class B
{
public static void main(String[] args)
{
System.out.println("This is main method of class B");
}
}
i hope this simple example will clear your doubt.
you can refer to link which contains Java tutorial for beginners.

Two public classes in one file java

Ok, this might be kiddies question in java. We can't define two public classes in one file. But, in one of the examples from the book SCJP study guide, this example was mentioned:
public abstract class A{
public abstract void show(String data);
}
public class B extends A{
public void show(String data){
System.out.println("The string data is "+data);
}
public static void main(String [] args){
B b = new B();
b.show("Some sample string data");
}
}
When I copy pasted this into netbeans immediately compile error was thrown, that public class A should me mentioned in separate file. Is that example from SCJP styudy guide really wrong? Also in some of the mock test I found many questions having such pattern but in none of the options was a compiler error was mentioned. Getting worried here
yes, 2 top level public classes are not allowed in one file
Well, if one is being so picky: you can have multiple classes defined with a public modifier in the same file, that is, using the static nested(inner) class.
like this:
File -> Test.java
public class Test {
public static class SomeNestedClass {
}
}
Yes you can have two classes in the same file. You can have them by removing the public access modifier from both the class name, like shown below,
abstract class A{
public abstract void show(String data);
}
class B extends A{
public void show(String data){
System.out.println("The string data is "+data);
}
public static void main(String [] args){
B b = new B();
b.show("Some sample string data");
}
}
you can make 2 public classes in one file , inside a class that contains them .
it's also recommended to add "static" for them , if you do not need any reference to the container class .
You can put two public classes in one file, for example in the file Circle.java:
public class Test {
public static void main(String args[]) {
double cir = Circle.findCircumference(7.5);
System.out.print("Circumference of circle=" + cir);
}
}
public class Circle {
public static double findCircumference(double radius) {
return 2 * Math.PI * radius;
}
}
If you then run javac Circle.java, you will get an error:
Circle.java:1: error: class Test is public, should be declared in a file named Test.java
public class Test {
^
1 error
But if you run it with java Circle.java, then it will work.
Why? Probably because the java command, since java 11 (see here), can run also single source-file programs.
Imagine you could place two public classes in one file, then think about the work of the compiler: it has to build a .class file from your .java file that represents exactly one class (otherwise the .class ending wouldn't make any sense).
The way the JAVA Compiler works it will simply create a .class file with the name of your file and will search for the class with the name of the file in your given file – so it depends on your file name which class will be correctly compiled and which will not.
Long story short: no, you can't put two public classes in one file because the compiler wouldn't be able to handle that correctly.
(Edit: it of course is possible to define new classes INSIDE the one public class that has the same name as your file.)

Categories

Resources