-3

The file compiles and works as expected but when I try to use it in another file it keeps saying when I attempt to compile. So clearly I don't know how to import code from another file correctly. How do I import the file for use within , I have read many explanations but they all require an IDE which I don't plan on using, I am using notepad++.

user3701257
  • 163
  • 2
  • 9

1 Answers1

3

You do import ReadFile.*; but ReadFile is not the package name but the class name. Package allows you to categorize your classes. Take a look at http://en.wikipedia.org/wiki/Java_package for more details.

Put your ReadFile.java into a package (for example: package org.your-company.io) then in the second class:

import org.your-company.io.ReadFile;

The instruction import somepackage.* indicates that you can use any classes from the package somepackage in your current class. For example, if I do:

import java.sql.*;

I will be able to call, in my code, directly:

Date dsql = ... // java.sql.Date
DriverManager driver = ... // java.sql.DriverManager
etc.

Edit

As Dukeling mentionned in a comment, if your classes are in the same "folder" (I mean package), you can remove your instruction import ReadFile.* which is wrong and useless.

lpratlong
  • 1,421
  • 9
  • 17
  • Good answer, but you should elaborate. What should he do? Why is that not working? – Anubian Noob Jun 03 '14 at 14:33
  • ... or if it's in the same package (which could be no package - which appears to currently be the case), there's no need to import it. – Bernhard Barker Jun 03 '14 at 14:35
  • What is a package? I just have a folder with three files: ReadFile.java, ReadFile.class and Centralise.java. How would I use ReadFile within Centralise? – user3701257 Jun 03 '14 at 14:37
  • So a package is just a folder. If your files are in the same folder, you don't need any imports. – Anubian Noob Jun 03 '14 at 14:39
  • @user3701257 : You can take a look on Oracle tutorial which will explain you the package concept: http://docs.oracle.com/javase/tutorial/java/package/ – lpratlong Jun 03 '14 at 14:42
  • Dukeling is right, you can simply remove your `import ReadFile.*` instruction and it should work. – lpratlong Jun 03 '14 at 14:43
  • Ok. I got rid of the import but I now get a new error: `Error: cannot find symbol` while pointing towards the line `ArrayList lines = ReadFile.openFile("text.txt");` more specifically pointing towards `ReadFile`. What do I do here? – user3701257 Jun 03 '14 at 14:43
  • you have to declare package name in all your classes (at the first line). For example, if you're in `org/your-company/io` folder, you will write `package org.your-company.io;`. I hope you understand what I mean. The best thing is to follow a tutorial. – lpratlong Jun 03 '14 at 14:47
  • @user3701257 That probably means Notepad++ is just trying to compile your file without considering other files in the same directory - you should perhaps read a (perhaps Notepad++ or perhaps generic) tutorial on compiling multiple Java files. Also, `openFile` isn't `static`, so you need an object to call it (you can't just say `ReadFile.openFile`) (or make it `static`), but that won't cause the error you're getting. – Bernhard Barker Jun 03 '14 at 14:50
  • Notepad++ is just a text editor, I am not compiling with it. I am using the command prompt in order to compile using the command `javac ReadFile.java` and `javac Centralise.java`. – user3701257 Jun 03 '14 at 14:52
  • I put everything within the folder Centralise then added the line `package Centralise;` to the top of each java file and it still gives me the same error @lpratlong – user3701257 Jun 03 '14 at 14:54
  • Are you sure you are compiling well? You can take info here: http://stackoverflow.com/questions/9511083/compiling-multiple-classes-console-in-java – lpratlong Jun 03 '14 at 14:57
  • Ohh right I was compiling separately, thanks. – user3701257 Jun 03 '14 at 15:04