[SOLVED] Java Include/Require constructs?

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

[SOLVED] Java Include/Require constructs?

Post by Chris Corbyn »

I'm googling this to no avail. How on earth can I include file A into file B so that file A can make use of classes in file B? :(

Basically like:

App.java

Code: Select all

include "MyClass.java"; //How do I do this?

class App
{
    public static void main(String[] args)
    {
        MyClass o = new MyClass();
        o.example();
    }
}
MyClass.java

Code: Select all

class MyClass
{
    public void example()
    {
        System.out.println("MyClass.example() called....");
    }
}
It's such a basic concept in any language I'm not sure how I haven't come across it early in the docs :(
Last edited by Chris Corbyn on Thu Oct 19, 2006 4:01 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I read that but now something's just clicked. If I name my files "<ClassName>.java" then the compiler will find them for me. If I make a package then I change my layout to "<PackageName>/<ClassName>.java". I can tell that it's loading the files in but I must be doing something a bit off because at first, if I do this:

App.java

Code: Select all

import myPackage.MyClass;

class App
{
        public static void main(String args[])
        {
                MyClass obj = new MyClass();
                obj.speak();
        }
}
myPackage/MyClass.java

Code: Select all

package myPackage;

class MyClass
{
        public MyClass() {}

        public void speak()
        {
                System.out.println("I'm speaking...");
        }
}
I get a compiler error about access issues (not public):

Code: Select all

[d11wtq@pc-cac TestApp]$ gcj --main=App -o runme App.java
App.java:1: error: Can't access class 'myPackage.MyClass'. Only public classes and interfaces in other packages can be accessed.
import myPackage.MyClass;
          ^
1 error
[d11wtq@pc-cac TestApp]$
So taking a stab in the dark I try updating myPackage/MyClass.java to declare MyClass as public:

Code: Select all

package myPackage;

public class MyClass
{
        public MyClass() {}

        public void speak()
        {
                System.out.println("I'm speaking...");
        }
}
Now getting the error:

Code: Select all

[d11wtq@pc-cac TestApp]$ gcj --main=App -o runme App.java
/tmp/ccm8ItPN.o: In function `App::main(JArray<java::lang::String*>*)':
App.java:(.text+0x16): undefined reference to `myPackage::MyClass::class$'
App.java:(.text+0x25): undefined reference to `myPackage::MyClass::MyClass()'
collect2: ld returned 1 exit status
[d11wtq@pc-cac TestApp]$
That at least tells me it's reading the MyClass.java file like I want it to; I just don't know why it's not working :(

Thanks for the help.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

put both files in same direcotry...remove those imports.

first compile MyClass
second the file that is using MyClass
should work.

edit:
also is this your first shot in Java. do you have environment well set and all.
e.g

Code: Select all

JAVA_HOME=javadir
export JAVA_HOME
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah it's my first shot at Java. I managed to successfully build a small app which used ten or so classes but they were all in the one file. It's breaking things into individual files which has thrown me.

So basically I need compile MyClass first, then compile and link App.

I removed the import/package keywords like you said, put them into one dir and gave it a shot but this happened:

Code: Select all

[d11wtq@pc-cac Foo]$ gcj -c MyClass.java && gcj --main=App App.java
/tmp/ccGkfXS3.o: In function `App::main(JArray<java::lang::String*>*)':
App.java:(.text+0x16): undefined reference to `MyClass::class$'
App.java:(.text+0x25): undefined reference to `MyClass::MyClass()'
collect2: ld returned 1 exit status
[d11wtq@pc-cac Foo]$ ls
App.java  MyClass.java  MyClass.o
[d11wtq@pc-cac Foo]$
It's obvious I don't know what I'm doing having been programming just about nothing but PHP/JavaScript for the past 3 years :oops: I'm not overly understanding of how compilers actually work (e.g. what the compiler does and what the linker does).

Did what I did above basically make an object file for MyClass and then try to compile and link App.java?

EDIT | I haven't set any environment variables. I've seen CLASSPATH used a lot but the compiler seems to find my files ok without that for now.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

My issue was to do with GCJ itself; not with my code. I never actually figured out the problem but I know it works with Blackdown compiling to bytecode.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You need to mark the class as public

Code: Select all

public class MyClass
{
	public MyClass() {}

	public void speak()
	{
		System.out.println("I'm speaking...");
	}
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

d11wtq wrote:So taking a stab in the dark I try updating myPackage/MyClass.java to declare MyClass as public:

Code: Select all

package myPackage;

public class MyClass
{
        public MyClass() {}

        public void speak()
        {
                System.out.println("I'm speaking...");
        }
}
Now getting the error:

Code: Select all

[d11wtq@pc-cac TestApp]$ gcj --main=App -o runme App.java
/tmp/ccm8ItPN.o: In function `App::main(JArray<java::lang::String*>*)':
App.java:(.text+0x16): undefined reference to `myPackage::MyClass::class$'
App.java:(.text+0x25): undefined reference to `myPackage::MyClass::MyClass()'
collect2: ld returned 1 exit status
[d11wtq@pc-cac TestApp]$
:)

I've started using blackdown and it's all hunky dory. I'll come back to GCJ with a bit more experience since I like the fact it compiles to machine code rather than .class files. I have to say; from the experimental code I've written so far I really like the language and the way it's structured :D

Is the forum on the sun website the best place to get java help?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops sorry, I only read the first part :-S
It compiles and runs without errors under jdk1.5.0_07/win32 so I'd rather look for a more gcc/gcj specific forum than java.sun ;)
Post Reply