Welcome to Dream.In.Code
Become a Java Expert!

Join 150,047 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,682 people online right now. Registration is fast and FREE... Join Now!




Making my classes available to myself

 
Reply to this topicStart new topic

Making my classes available to myself

RicardoDiaz
12 Jun, 2008 - 10:07 AM
Post #1

New D.I.C Head
*

Joined: 12 Aug, 2007
Posts: 10


My Contributions
Hi, I made a class and I need to be able to import it to other files. Does it have to be in a package? If so, how can I do it? I tried putting the statement package pkgName on top of the file and putting it in a folder named pkgName. But when I try to import it from another file, I get an error saying that the package pkgName doesn't exist.

So, then, I tried using export CLASSPATH=(path to the folder where pkgName resides) but I got the same error. How can I make my class available to other files?

BTW I'm using Mac OS X 10.5 and Terminal.
User is offlineProfile CardPM
+Quote Post

mensahero
RE: Making My Classes Available To Myself
12 Jun, 2008 - 10:09 AM
Post #2

c0mput3rz Are Only Human
Group Icon

Joined: 26 May, 2008
Posts: 664



Thanked: 17 times
Dream Kudos: 75
My Contributions
Hello.. blink.gif

I think you can copy/paste the class file into your project src directory.. and that should work.. blink.gif please correct me if i'm wrong..

This post has been edited by mensahero: 12 Jun, 2008 - 10:09 AM
User is offlineProfile CardPM
+Quote Post

abgorn
RE: Making My Classes Available To Myself
12 Jun, 2008 - 10:31 AM
Post #3

Hello Crap for Brains
Group Icon

Joined: 5 Jun, 2008
Posts: 912



Thanked: 5 times
Dream Kudos: 50
My Contributions
Ye, copying it to src should be fine.
User is offlineProfile CardPM
+Quote Post

pbl
RE: Making My Classes Available To Myself
12 Jun, 2008 - 02:44 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(abgorn @ 12 Jun, 2008 - 11:31 AM) *

Ye, copying it to src should be fine.

If they have the same package name or no package statement.
User is online!Profile CardPM
+Quote Post

RicardoDiaz
RE: Making My Classes Available To Myself
12 Jun, 2008 - 08:28 PM
Post #5

New D.I.C Head
*

Joined: 12 Aug, 2007
Posts: 10


My Contributions
I copied the class file to the directory but I get an error saying "cannot find symbol: variable IO" when I compile a java file that tries use that class. How should the class be written for it to be able to be used like: IO.doSomething()? (where doSomething() is a static method)
User is offlineProfile CardPM
+Quote Post

pbl
RE: Making My Classes Available To Myself
12 Jun, 2008 - 09:37 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(RicardoDiaz @ 12 Jun, 2008 - 09:28 PM) *

I copied the class file to the directory but I get an error saying "cannot find symbol: variable IO" when I compile a java file that tries use that class. How should the class be written for it to be able to be used like: IO.doSomething()? (where doSomething() is a static method)

copy your .java file and recompile it
User is online!Profile CardPM
+Quote Post

RicardoDiaz
RE: Making My Classes Available To Myself
12 Jun, 2008 - 09:54 PM
Post #7

New D.I.C Head
*

Joined: 12 Aug, 2007
Posts: 10


My Contributions
I've done that and I get the same error. Here's the class I'm trying to make available:

CODE
import java.io.*;
import javax.swing.JFileChooser;

public class IO
{    
    public static void print(Object string)
    {
        System.out.print(string);
    }
    
    public static void println(Object string)
    {
        System.out.println(string);
    }
    
    public static String readln() throws IOException
    {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        return stdin.readLine();
    }
    
    public static String readlnFromFile(String file) throws IOException
    {
        String readedString = "";
        
        try {
            BufferedReader stdin = new BufferedReader(new FileReader(file));
            readedString = stdin.readLine();
        } catch (IOException ex) {
            
        }
        
        return readedString;
    }
    
    public static String readlnFromUserSelectedFile() throws IOException
    {    
        String readedString = "";
        File file;
        
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Select file to read from");
        fileChooser.showOpenDialog(null);
        
        file = fileChooser.getSelectedFile();
        
        try {
            BufferedReader stdin = new BufferedReader(new FileReader(file));
            readedString = stdin.readLine();
        } catch (IOException ex) {
            
        }
        
        return readedString;
    }
    
    public static void writeToFile(String file, String string) throws IOException
    {
        try {
            BufferedWriter stdout = new BufferedWriter(new FileWriter(file));
            stdout.write(string);
            stdout.close();
        } catch (IOException ex) {
            
        }
    }
    
    public static void writeToUserSelectedFile(String string) throws IOException
    {
        File file;
        
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("Select file to write to");
        fileChooser.showOpenDialog(null);
        
        file = fileChooser.getSelectedFile();
        
        try {
            BufferedWriter stdout = new BufferedWriter(new FileWriter(file.getName()));
            stdout.write(string);
            stdout.close();
        } catch (IOException ex) {
            
        }
    }
}


And here's the file that tries to use the class: (This is the file that won't compile)

CODE
public class Test
{
    public static void main(String args[])
    {
        IO.println("Hello!");
    }
}


The error I get says that the symbol: "variable IO" cannot be found. Am I doing something wrong?



User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Making My Classes Available To Myself
12 Jun, 2008 - 10:56 PM
Post #8

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
First Compile the IO.java File before you try and Run the Test.java(java Test) File. (Assuming you have kept the files in the same directory)

I tried it and it worked ...

P.S Just the IO.class file in the same directory would do !!

This post has been edited by neotrumatrix: 12 Jun, 2008 - 10:58 PM
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Making My Classes Available To Myself
12 Jun, 2008 - 11:10 PM
Post #9

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
If it is a stand alone "library" that you are going to use in several projects, then jar it and import it into the project you want to use it.
User is offlineProfile CardPM
+Quote Post

RicardoDiaz
RE: Making My Classes Available To Myself
13 Jun, 2008 - 11:04 AM
Post #10

New D.I.C Head
*

Joined: 12 Aug, 2007
Posts: 10


My Contributions
Ok I finally got it to work. What I was doing wrong is that I was compiling the test file with an application I made with Cocoa that used "javac (absolute path to source file)" to compile. But for javac to see the class file it has to be run from the directory where the class file is.

Thanks for all the help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:10PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month