Java setup - basic (not very simple yet ...) Question

I recently switched to Java with Head First Java, and I found that the book did not have much information about setting up Java. This pretty much told me to download the JDK and mentioned something about the classpath variable.

I was very confused, and obviously my Java was not set up to work. So ... I researched and was able to add the / bin / directory to my CLASS_PATH, and also created a new JAVA_HOME system variable and pointed it to the bin folder. I'm not quite sure what to do - and this topic seems to be easy for any other programmer because I cannot find anyone else having difficulty setting up their Java environment.

So now I can compile and run the programs. I did some things Hello World, 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 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 (object?) - MovieTestDrive.java, I return this error:

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

I did some research, and from what I have compiled, I think Java does not know how to look for the first class. However, my research found almost no useful information on how to direct a stupid thing to where it is.

+4
source share
8 answers

Is there any special reason why you are not using an IDE like Eclipse , IntelliJ IDEA or Netbeans ?

+4
source

Try making the definition of the PlayIt () method publicly available as follows:

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

then recompile and try again.

Your classes may not be in the same package.

In addition, the main method that you specified does not look right. You did not announce two and three! You need to declare them and instantiate Movie objects before using them.

+3
source

You are missing a semicolon 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 ; } } 
+2
source
  • JAVA_HOME should point to the root directory of the JDK, not the bin folder.
  • PATH should point to the JDK bin folder, i.e. JAVA_HOME\bin .
  • To point to the jars and classes folder, add it to the CLASSPATH variable, not CLASS_PATH .
  • Each declaration of an announcement and method calls end with a semicolon ( ; );

In addition, you call two.playIt(); (small p in playIt() ), and the playIt() method has capital p . Java is case sensitive, so every method, variable, class, etc. Must be called / used with the correct case.

0
source

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. Usually this directory has a bin directory in it, which contains javac and a jre directory in it.

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

 javac -classpath . MovieTestDrive.java 

A period indicates that the current working directory is the root of the class tree.

Please note that you really need to do a lot more so that your programs (even if they were just toys) work much better in the Java environment. Development in the "default" package is not recommended. To fix your default development, add a line at the beginning of each .java file.

 package org.myname.movie; 

and create directories matching

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

and then move the files to the appropriate directories.

As a bonus, you quickly want a script along with your build system to prevent the need to compile java type commands. I recommend ant as a build system for Java, but if you have a background with make , you can use it until you get comfortable with ant.

Other good ideas are to share the source code directory with your directory in which your compiled classes are stored. Javac has a command line option "destdir" that will place the ".class" files in a different directory tree than your sources. This makes it easy to perform full recovery (deleting classes without sacrificing sources) and sets you up for convenient packaging (which you will ultimately need).

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

0
source

You probably need to add your current directory to your classpath, if you use -empty-package by default and compile in the same directory, you have java sources and compiled classes. Try adding -classpath . to your javac command line.

0
source

I think the problem is that there are compilation errors in your class.

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

Compile both 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

0
source

You are missing two and three movie ads and half-columns.

 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 must have a lowercase "o". eg.
  System.Out.println("Playing the movie"); 

The first running file is MovieTestDrive.java, because it is an open class with the main method, and movie.java is not. Therefore, you need to run the PlayIt () method in the MovieTestDrive file.

NOTE: this works in eclipse ( Eclipse Download )

0
source

All Articles