wood burning stoves*
The moose likes IDEs, Version Control and other tools and the fly likes Setting up two src files in Netbeans IDE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Setting up two src files in Netbeans IDE" Watch "Setting up two src files in Netbeans IDE" New topic
Author

Setting up two src files in Netbeans IDE

Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Hello,
I'm trying to get a minesweepers game found here: http://zetcode.com/tutorials/javagamestutorial/minesweeper/
set up in the Netbeans IDE. Theres two .java files, Mines.java, which is the main class, and Board.java, which holds most of the code.

Do I start a new project, mines, in the IDE, then somehow reference the Board.java file? Or does all the src go in one file somehow?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

They can both go into the same class since the second class is not public. Make sure the file name matches the public class name. And that the package name matches the one in the code.

You'll need to create a NetBeans project for your file. See the quickstart for how.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Started a new project named Mines in the Netbeans IDE, so the code now looks like this:

package mines;

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Mines extends JFrame {

private final int WIDTH = 250;
private final int HEIGHT = 290;

private JLabel statusbar;

public Mines() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLocationRelativeTo(null);
setTitle("Minesweeper");

statusbar = new JLabel("");
add(statusbar, BorderLayout.SOUTH);

add(new Board(statusbar));

setResizable(false);
setVisible(true);
}

public static void main(String[] args) {
new Mines();

}
}

So your saying the Boards.java code is supposed to go right after:

public static void main(String[] args) {
new Mines();
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Tryed setting up the new project "from existing sources" in the IDE, using the Mines.java and Board.java files held in a C:\JavaProject\Mines\MinesSource folder.

This loaded both sources into the IDE Project section.

However, the first line of each source is underlined in red giving an "incorrect package" error. Any ideas on why that is?
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Larry Evers wrote:Tryed setting up the new project "from existing sources" in the IDE, using the Mines.java and Board.java files held in a C:\JavaProject\Mines\MinesSource folder.

This loaded both sources into the IDE Project section.

However, the first line of each source is underlined in red giving an "incorrect package" error. Any ideas on why that is?


Just check that both the classes are in the package, they are supposed to be!!

Plus, I would suggest you to write your own code, rather than getting a code from internet, and running it.. This will help you understand better..
And start with notepad and Command Line.. Don't directly get to an IDE.. This I am saying assuming that you are a beginner in Java..
You will understand whole point of packages and other stuffs better..
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Thanks for the reply. Speaking as a VB programmer, the Java code itself is not too bad from what I can tell so far, but the IDE/compiler are taking some getting used to. I managed to get the Snakes game at that url running ok. The Minesweeper game will have to wait as I have to manufacture its icons. I'm looking at these practical examples as I can rewrite and modify them to get a better feel for the code, as well as trying to get a handle on the IDE/compiler. Unfortunately the IDE tutorials I've seen are very generic. Will note my progress with the games mentioned here shortly. I'm really only at my fourth Java program, so things aren't going too bad.
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Wasn't used to using multiple source .java files, but I got the Snake and Breakout games to work by using the following:

Start a new project, java with existing sources.
Project name: Snake
Project folder: C:\JavaProject\Snake
Buildscript name: build.xml
checkmark "set as main project"
Source package folders: C:\JavaProject\Snake\SnakeSources

in that last directory I had:
apple.png
Board.java
dot.png
head.png
Snake.java

The IDE says: Project Snake does not have a main class set, select the main class: Snake

In the IDE Project window, the Snake.java and Board.java both show errors if the first line of each: "package snake;" is left in. If taken out, the source builds and runs ok.

Note I used freeware icons in .png format, edited by Real World Icon Editor found on the web.

So, I now can edit and run Java programs that have multiple src files.

However, I still am having a problem with setting up the package name in the IDE when I start a new project. I can only get the project to run if I use the default package, not the "package Snake;" specified in the source code files.

Any ideas on this last problem welcome.

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5836
    
    7

Larry Evers wrote:In the IDE Project window, the Snake.java and Board.java both show errors if the first line of each: "package snake;" is left in. If taken out, the source builds and runs ok.

The package declaration must match the names of the directories in which the source files are located. Thus your source files must be at:

C:\JavaProject\Snake\SnakeSources\snake\Snake.java
C:\JavaProject\Snake\SnakeSources\snake\Board.java

NetBeans should have placed a red underling under the "package snake;" line in the source file. If you hit ALT-ENTER, it would have provided tow options to fix this error, one of which is "Move class to the correct folder". That will do the work of creating the "snake" folder and moving the .java files there.


JBoss In Action
Larry Evers
Greenhorn

Joined: Aug 07, 2012
Posts: 14
Thanks Peter, your correct. I saved the sources and .png files, then deleted the project. Started again with the files in the new directory structure, and the IDE no longer showed a red line under "package snake;" as it has before. The project built and ran ok. Still going through "Java: The Complete Reference", with a couple of other Java books after that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting up two src files in Netbeans IDE