Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Importing packages not working for some reason.

No idea why this is not working, help appreciated!

  • Console message
āžœ  com git:(master) āœ— clear && javac Karaoke.java && java Karaoke

Karaoke.java:1: error: package com.teamtreehouse.model does not exist
import com.teamtreehouse.model.Song;
                              ^
Karaoke.java:2: error: package com.teamtreehouse.model does not exist
import com.teamtreehouse.model.SongBook;
                              ^
Karaoke.java:7: error: cannot find symbol
    Song song = new Song("Michael Jackson", "Beat It", "https://www.youtube.com/watch?v=T2PAkPp0_bY");
    ^
  symbol:   class Song
  location: class Karaoke
Karaoke.java:7: error: cannot find symbol
    Song song = new Song("Michael Jackson", "Beat It", "https://www.youtube.com/watch?v=T2PAkPp0_bY");
                    ^
  symbol:   class Song
  location: class Karaoke
Karaoke.java:8: error: cannot find symbol
    SongBook songBook = new SongBook();
    ^
  symbol:   class SongBook
  location: class Karaoke
Karaoke.java:8: error: cannot find symbol
    SongBook songBook = new SongBook();
                            ^
  symbol:   class SongBook
  location: class Karaoke
6 errors
  • Song.java
package com.teamtreehouse.model;

public class Song {
  private String mArtist;
  private String mTitle;
  private String mVideoUrl;

  public Song(String artist, String title, String videoUrl) {
    mArtist = artist;
    mTitle = title;
    mVideoUrl = videoUrl;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getArtist() {
    return mArtist;
  }

  public String getVideoUrl() {
    return mVideoUrl;
  }

  @Override
  public String toString() {
    return String.format("Song: %s by %s", mTitle, mArtist);
  }
}
  • SongBook.java
package com.teamtreehouse.model;

import java.util.ArrayList;
import java.util.List;

public class SongBook {
  private List<Song> mSongs;

  public SongBook() {
    mSongs = new ArrayList<Song>();
  }

  public void addSong(Song song) {
    mSongs.add(song);
  }

  public int getSongCount() {
    return mSongs.size();
  }
}
  • Karaoke.java
import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;

public class Karaoke {

  public static void main(String[] args) {
    Song song = new Song("Michael Jackson", "Beat It", "https://www.youtube.com/watch?v=T2PAkPp0_bY");
    SongBook songBook = new SongBook();
    System.out.printf("Adding %s %n", song);
    songBook.addSong(song);
    System.out.printf("There are %d songs. %n", songBook.getSongCount());
  }
}

The project structure: https://i.gyazo.com/438445ae9f8255890595ba4f03017060.png

Hi,

Can you take a snapshot of your workspace and paste the link in here. I'll see if I can fix the issue that way. I always get confused looking at the structure! I'll fork your code and get it working, hopefully.

Steve.

Hi Steve,

I'm using Atom to edit the code and linux terminal to compile/execute the code.

Soooryy - I'd assumed you were in Workspaces. I'll give it some more thought but this always fries my mind!

To clarify what the file structure looks like.

KaraokeMachine/
  com/Karaoke.java // meaning that inside com is Karaoke.java and teamtreehouse folder
    teamtreehouse/ // Nothing here (other than model folder)
      model/Song.java, SongBook.java // Inside model Song.java and SongBook.java

Here's a snapshot of this, it isn't my snapshot, it is someone else's code I was helping out in the Community pages. The import & package seems the same at first glance ...

https://w.trhou.se/wehho6udle

Found this - any help?

Steve.

I just ran my code on the work spaces, it works. But for some reason this is not working at all on my PC.

I checked out that link earlier, seemed like a bunch of gibberish to me LOL, I'll go through it again.

So nothing wrong with the code, just some random problem with the project structure.

I think the Stack Overflow post is talking about where you are running the compile command from. You can specify a classpath which tells the compiler where to expect to find code to compile and create your executable code.

This is all far easier in something like Netbeans, Eclipse or Intelli-J - these IDEs will fix your imports for you.

Steve.

I just opened the project in IntelliJ and I cannot even run it, it's grayed out.

Trying to optimize imports, (CTRL + ALT + O)But it says that "Unused imports not found"

I'm just gonna keep going and keep pushing the code to Github even though it doesn't work on my pc, no solution found.

The Github repo has all your TH stuff in it, not just this project. I don't think that'll clone easily as it isn't one contained application. I can't just clone the Karaoke part of it.

1 Answer

Hi there,

I pulled your Github repo into Intelli-J. It errored with the package tree.

As above, with the classpath, deleting the com. part of the package and import statements works fine.

This is because Karaoke is within com. so it doesn't need specifying - this is the root of the compiled classpath.

Make sense?

Steve.

P.S. Pull request sent in Github. :smile:

YES! I Added the Karaoke.java inside of the com folder instead of KaraokeMachine folder.

This is working now. Updated github as well.

Thank you Steve for the help!

Good to get it fixed! :+1: