Reputation: 61
I am having some issues with a program not finding the needed classes, although they have the same directory and the same package declaration.
Eclipse says "can't find or run main class", so I tried to compile it manually. Here's what the compiler says:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.
C:\Users\Gilbert>cd C:\Users\Gilbert\Desktop\factory
C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" P
rodukt.java
C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" S
tation.java
C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" L
ine.java
Line.java:8: error: cannot find symbol
private Produkt produkt;
^
symbol: class Produkt
location: class Line
Line.java:9: error: cannot find symbol
private ArrayList<Station> stations;
^
symbol: class Station
location: class Line
Line.java:12: error: cannot find symbol
public Line(String name, Produkt produkt){
^
symbol: class Produkt
location: class Line
Line.java:17: error: cannot find symbol
public void add(Station station){
^
symbol: class Station
location: class Line
Line.java:25: error: cannot find symbol
public Station getStation(int number){
^
symbol: class Station
location: class Line
5 errors
Here's the Code (of the "tried to compile" classes, no main class included):
File 1:
package factory;
public class Produkt{
private String name;
public Produkt(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
}
File 2:
package factory;
public class Station{
private int number;
private int time;
public Station(int number, int time){
this.number = number;
this.time = time;
}
public int getNumber(){
return this.number;
}
public void setNumber(int number){
this.number = number;
}
public int getTime(){
return this.time;
}
public void setTime(int time){
this.time = time;
}
}
File 3:
package factory;
import java.util.ArrayList;
public class Line{
private String name;
private Produkt produkt;
private ArrayList<Station> stations;
private int time;
public Line(String name, Produkt produkt){
this.name = name;
this.produkt = produkt;
}
public void add(Station station){
stations.add(station.getNumber(), station);
}
public String getName(){
return this.name;
}
public Station getStation(int number){
return stations.get(number);
}
public String getProduktName(){
return produkt.getName();
}
public int getTime(){
for (int i = 0; i < stations.size(); i++){
this.time = this.time + stations.get(i).getTime();
}
return this.time;
}
public int getStations(){
return stations.size();
}
}
A download-Link for the full program: https://dl.dropboxusercontent.com/u/34761768/factory%20-%20stack%20overflow.zip
Edit:
Ok so here's the main class:
package factory;
public class Main{
public static void main(String[] args){
Factory factory = new Factory("");
factory.reader(args[0]);
factory.print();
System.out.println("----");
factory.printBestWayOf2();
System.out.println("----");
factory.printBestWay();
}
}
It uses yet another class that i didnt show because it is the longest of them all. I didnt show the main class because the error occures either i try to compile the main class or another. I can Only compile those classes that dont have any reference to other classes being part of the package, such as produkt and station, but any other class cant find them, although they are ALL part of the package and all inside the directoy.
Upvotes: 0
Views: 411
Reputation: 208994
You need a class with a main
method.
You are getting all those errors compiling individually because the classes are dependent on one another. You need to compile all at the same time with javac *.java
Event after doing 2, your program won't run, because 1.
In case you don't know, this is a main
method
public static void main(String[] args) {
}
Create another class to run the app. Something like this
public class App {
public static void main(String[] args) {
Produkt p = new Produkt("product");
Line line = new Line("line", p);
line.add(new Station(1, 2));
....
}
}
Other notes.
Not sure you want this
public void add(Station station){
stations.add(station.getNumber(), station);
}
The add
method you are using, the first argument is the index of the ArrayList
. That will cause you problems, and the index probably wont exist
add(int index, E element)
Inserts the specified element at the specified position in this list.
Just do
stations.add(station);
You should initialize stations
in the constructor. If you don't you will face the wrath of the NullPointerException
public Line(String name, Produkt produkt){
this.name = name;
this.produkt = produkt;
station = new ArrayList<Station>();
}
If you wanted to print an object later, you may want to override toString
in your classes.
UPDATE with the rest of your code
Don't don't don't compare Strings with ==
. Use equals
if (parts[0] == "factory")
Change to
if ("factory".equals(parts[0]))
Change ALL of them.
I have problem compiling your code.
You may also need to set the launch configuration. You need to right-click on your project, and go to properties. Click Run/Debug Settings. If your Main
class is not in that list, then click New, then Java Application, then find your class an make sure it is the Main Class. Make sure you use the fully qualified name e.g. mypackage.Main
Upvotes: 2
Reputation: 61
OK so i solved it, but I still have a problem with my solution:
If i take the main Class out of the package, and import it (import factory.*;) I can compile and run it, but my task forbidds me to use the default package. Doesn't a main class outside of my own package break that rule?
Thx for answers!
Upvotes: -1
Reputation: 371
Is there a reason why you do not include the main class with this question?
The application needs
public static void main(Strings args[]){
}
to compile however lets talk about you trying to run from the command line. cd - change directory C:\Users\Gilbert\Desktop\factory>"C:\Program Files\Java\jdk1.7.0_45\bin\javac" P rodukt.java
okay so, you changed the directory to C:\Users\Gilbert\Desktop\factory then for some reason in quotation marks you list "C:\Program Files\Java\jdk1.7.0_45\bin\javac", why? stage 1: change directory to the actual location of the class file, this class file should be the class that contains the :
public static void main(String args[]){
}
so in other words the following should be done cd C:\Users\Gilbert\Desktop\factory\the actual folder that contains your main class here
javac Main.java(this compiles the Main class)
Upvotes: 1