简体   繁体   中英

Setting Up Java - Basic (yet not so basic…) Question

I've recently jumped into Java with Head First Java, and I found that the book lacked a lot of information on setting up Java. It pretty much told me to download the JDK, and mentioned something about a classpath variable.

I was very confused, and obviously, my Java wasn't set up to function. So... I researched, and managed to add the /bin/ directory to my CLASS_PATH, and I also made a new system variable called JAVA_HOME and pointed that to the bin folder. I'm not really sure what to do - and this topic is seemingly easy for every other programmer, because I can't find anyone else having this much difficulty setting up their Java environment.

So, I can compile and run programs now. I've done some Hello World stuff, yada yada. Now, on page 37, we're actually going to do some OOP stuff. Here are the classes:

class Movie {
    String title;
    String genre;
    int rating;

    void PlayIt() {
        System.Out.println("Playing the movie");
    }
}

and the second:

public class MovieTestDrive  {
    public static void main(String[] args) {
        Movie one = new Movie();
        one.title = "Movie 1";
        one.genre = "Movie 1 Genre";
        one.rating = -2
        two.title = "Movie 2";
        two.genre = "Movie 2 Genre";
        two.rating = -1
        three.title = "Movie 3";
        three.genre = "Movie 3 Genre";
        three.rating = 3
    }
}

So, I can compile the first class (Movie.java). However, when I try to compile the second class (the object?) - MovieTestDrive.java, I'm returned this error:

MovieTestDrive.java:12: cannot find symbol
symbol : method PlayIt()
location: class Movie
two.playIt();

1 error

I've done some research, and from what I've gathered, I guess Java doesn't know to also look for the first class. However, my research did not find nearly any helpful information at all on how to guide the silly thing to where it is.

Is there any particular reason that you're not using an IDE such as Eclipse , IntelliJ IDEA or Netbeans ?

Try chaning the definition of PlayIt() method to public like this:

  public void PlayIt() {
    System.out.println("Playing the movie");
  }

then recompile and try again.

It may be that your classes are not in the same package.

Also, the main method that you provided looks wrong. You did not declare two and three. You need to declare these and instantiate Movie objects before you use them.

You are missing some semicolons and some declarations.

public class MovieTestDrive  {
    public static void main(String[] args) {
        Movie one = new Movie();
        Movie two = new Movie(); // <-- missing declaration
        Movie three = new Movie(); // <-- missing declaration
        one.title = "Movie 1";
        one.genre = "Movie 1 Genre";
        one.rating = -2; // <-- Missing ;
        two.title = "Movie 2";
        two.genre = "Movie 2 Genre";
        two.rating = -1; // <-- Missing ;
        three.title = "Movie 3";
        three.genre = "Movie 3 Genre";
        three.rating = 3; // <-- Missing ;
    }
}
  1. JAVA_HOME must point to JDK root directory and not the bin folder.
  2. PATH must point to the JDK bin folder, ie JAVA_HOME\bin .
  3. To point to jars and classes folder, add it to CLASSPATH variable and not CLASS_PATH .
  4. Every variable declaration and method calls ends with a semicolon ( ; );

Also, you're calling two.playIt(); (small p in playIt() ) and PlayIt() method has a capital P . Java is case-sensitive, so every method, variable, class, etc. must be called/used with the correct case.

JAVA_HOME should not point to the bin folder in most cases, it should point to the root directory of your Java distribution. In a development environment, this is the root of the JDK. Normally this directory has a bin directory in it which contains javac, and a jre directory in it.

When compiling your second class, it cannot find your first class because you forgot to tell the compiler where the classes for this project are located. Javac will add classes to the classpath, but it will not discover classes independently. You need to do something like:

javac -classpath . MovieTestDrive.java

The "dot" indicates that the current working directory is the root of a class path tree.

Note that you really need to do a lot more to make your programs (even if they are just toy projects) work much better in a Java environment. Development in the "default" package is not advised. To fix your "default" package development, add the line to the top of each.java file

package org.myname.movie;

and make the directories that correspond

./org
./org/myname
./org/myname/movie

and then move the files into their appropriate directories.

As a bonus, you will quickly want to script together your build system to prevent the need to hand type java compilation commands. I recommend ant as a build system for Java, but if you have prior history with make you might want to use it until you become proficient with ant.

Other good ideas is to separate your source code directory with your directory holding your compiled classes. Javac has a command line option "destdir" which will place the ".class" files in a different directory tree than your sources. This allows for easy full rebuilds (removing the classes without damaging the sources) and will set you up for easy jar packaging (which you'll eventually want to do).

Good luck, and if you need help, feel free to comment.

Probably you need to add your current directory to your classpath, if you are using the default -empty- package and compiling in the same directory you have the java sources and the compiled classes. Try adding -classpath. to your javac command line.

I think the problem is there are compile errors on your class.

System.Out in Movie should be System.out for example

Compile both the java classes first and then try to run.

javac Movie.java

javac MovieTestDrive.java

Then when they both compile, run then using: java -cp. MovieTestDrive

You are missing the declarations of movie two and three and the semi-colons.

public class MovieTestDrive  {
    public static void main(String[] args) {
        Movie one = new Movie();
        one.title = "Movie 1";
        one.genre = "Movie 1 Genre";
        one.rating = -2;
        Movie two = new Movie();
        two.title = "Movie 2";
        two.genre = "Movie 2 Genre";
        two.rating = -1;
        Movie three = new Movie();
        three.title = "Movie 3";
        three.genre = "Movie 3 Genre";
        three.rating = 3;
        two.PlayIt(); // Try adding this once fixing the code.
    }
}

Also in

 System.Out.println("Playing the movie");
you should have a lower case 'o' in out. eg

 System.out.println("Playing the movie");

The first file that is run is the MovieTestDrive.java because it is the public class with a main method while the movie.java file does not. Therefore you need to run the PlayIt() method in the MovieTestDrive file.

NOTE: this works in eclipse ( Eclipse Download )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM