//Vector.java
package simple;
public class Vector{
public Vector(){
System.out.println("net.mindview.simple.Vector");
}
}
//List.java
package simple;
public class List{
public List() {
System.out.println("net.mindview.simple.List");
}
}
//LibTest.java
import simple.*;
public class LibTest{
public static void main(String[] args) {
Vector v = new Vector();
List l = new List();
}
}
When I try to set the classpath for Vector or List,
like
java classpath "C:\Learning Java\AccessControl" simple.Vector, I could Main method could not be found, please define main method. But in the book I'm using, neither file needs to have a main method.
If I try to run LibTest I get cannot access Vector and class file contains wrong class:Vector, errors.
From your posted code, nor Vector or List classes has the public static void main(String[] args) method in it, thus you getting the error.
Note that LibTest class has it, so it would be better to execute this class:
java classpath "C:\Learning Java\AccessControl" other.package.LibTest
To run package first you have to compile it from the directory
ex., C:\package-name\abc.java
package-name: as you mentioned simple.
After it you have to run "abc.java" file from the directory.
ex., C:____
Hope it will work
Related
//: 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 compiled this code and then, TestInner$1.class emerged.
I know ~~~$1.class indicates that file has "anonymous class."
But I don't understand the reason why this class file made. I want to know the reason.
Here is the code.
public class TestInner {
private static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I tried another version removed "private" identifier, like the following.
public class TestInner {
static class Inner { }
public static void main(String[] args){
new Inner();
}
}
I'd imagined that this code also would make TestInner$1.class file.
However it didn't create the file.
In addition, the following code, added Constructor, also didn't make TestInner$1.class.
public class TestInner {
private static class Inner {
Inner(){ }
}
public static void main(String[] args){
new Inner();
}
}
I have no idea, so can anyone help me?
EDIT:
I found the same question and it solved. Thank you for your helping.
Why is an anonymous inner class containing nothing generated from this code?
None of your examples have anonymous inner classes. None of them will produce a file named TestInner$1.class. All of them will produce a file named TestInner$Inner.class.
The following example shows an anonymous inner class and will produce TestInner$1.class:
public class TestInner {
public static void main(String[] args){
new Object() {
#Override public String toString () {
return "ninja";
}
};
}
}
I'm not sure where your TestInner$1.class came from but I'm guessing it's left over from previous experiments you were doing.
Update 1: I can confirm that without using Eclipse I get TestInner$1.class (in addition to TestInner$Inner.class -- 3 files are produced) for the first example but not for the last two, just like you are seeing. Will update when I find out why. When compiled via Eclipse, TestInner$1.class is never produced.
Update 2: OP found solution in Why is an anonymous inner class containing nothing generated from this code?.
I thought I reasonably understood the use of packages but am experiencing an ostensibly trivial issue when attempting to use a method from an imported package.
I have three files in the following directory structure:
Tester.java
approach1\Approach.java
approach2\Approach.java
Their code is as follows:
Tester.java
import approach1.Approach;
public class Tester {
public static void main(String[] args)
{
approach1.Approach.sharedMethod("TEXT");
sharedMethod("TEXT");
}
}
approach1\Approach.java
package approach1;
public class Approach {
public static void sharedMethod(String approachText)
{
System.out.println("Approach Text: " + approachText);
}
}
approach2\Approach.java
package approach2;
public class Approach {
public static void sharedMethod(String approachText) { }
}
As you can likely guess, I'm trying to elicit different responses from the different approaches based on what package/class is imported. The problem I encounter is within Tester.java. The first, explicit line works fine whereas the second, imported line (sharedMethod("TEXT")) throws an error of "The method sharedMethod(String) is undefined for the type Tester". I don't understand as I have imported one of the packages, so the method should be visible.
Any clarification would be appreicated as I'm a Java newb. Thanks!
You could import your static method shareMethod like this
import static approach1.Approach.sharedMethod;
the standard kind of imports that you have used only import the classes - so everything within a class must be referenced using the class name. just use:
Approach.sharedMethod()
and now the compiler will be able to know which method to use all depending on which Approach class you have imported at the top.
Just to clarify:
import approach1.Approach;
public class Tester {
public static void main(String[] args)
{
Approach.sharedMethod("TEXT");
}
}
is different from
import approach2.Approach;
public class Tester {
public static void main(String[] args)
{
Approach.sharedMethod("TEXT");
}
}
You should only specify the class name and leave it to the package import statement at the top to determine which package to find the class/methods from.
You only need to explicitly mention the package in the main program body if there is a conflict in names or if you have not imported anything.
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.)
Im having issues now with static and non staic errors. with sertain variables unable to find the main method.
I have strated the program by creating a seperate file which instantiates the class. like so:
public class StartUp {
public void main(String[] args) {
MainDriver theMainDriver = new MainDriver();
theMainDriver.start();
}
}
Within certain classes in the program it passes variables back to the mainDriver. But when I try to refrence it to, I get the error "cannot find symbol variable theMainDriver" .
e.g:
public void getEmployee() {
theMainDriver.setEmployee(theEmployee);
}
public void getEmployeeID() {
theMainDriver.setEmployeeID( randomIDno);
}
how can I declare the main Driver in a way that makes it more visible to other classes.
It does find the main driver if I do this MainDriver.setEmployeeID( randomIDno); but then it has issues with non static method cannot be referenced from a static context.
You can do the following:
public class StartUp {
public static MainDriver theMainDriver;
public void main(String[] args) {
theMainDriver = new MainDriver();
theMainDriver.start();
}
}
And call from any class as follows:
Startup.theMainDriver.setEmployeeID(randomIDno);
3 things if there was a compilation error.
1) Is MainDriver Public?
2) if MainDriver is in different package, did u import it ?
3) If MainDriver is from diffrent pacakage or different project or a external jar, Did u give that in the classpath ?
also, as the othe guy said, u for got the static part of the main(String[] args)
Firstly, the declaration of main should be like
public static void main(String[] args)
I cant understand how you are running the programm without making it static
Secondly, You didn.t gave the structure of your MainDriver class. May be it is not public. So make your MainDriver class public. I think that will resolve the problem