By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,353 Members | 1,529 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,353 IT Pros & Developers. It's quick & easy.

compiling java programs in Unix

P: 2
I'm having trouble compiling my program in Unix. I initially developed it on Windows in Eclipse. I have two files: Tokenizer.java and TokenizerMain.java. They are both in a directory called "tokenizer" I'm compiling from my home directory:

% javac tokenizer/TokenizerMain.java
./tokenizer/Tokenizer.java:5: 'class' or 'interface' expected
package tokenizer;
^
tokenizer/TokenizerMain.java:2: cannot access tokenizer.Tokenizer
bad class file: ./tokenizer/Tokenizer.java
file does not contain class tokenizer.Tokenizer
Please remove or make sure it appears in the correct subdirectory of the classpath.
import tokenizer.Tokenizer;
^
2 errors

And those are the errors I receive. It works fine when I compile it via Eclispe.
Under my TokenizerMain class I typed:
package tokenizer;
import tokenizer.Tokenizer;
Under my Tokenizer class I typed:
package tokenizer;

Any help would be appreciated.
Nov 14 '08 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
First, when using javac to compile your .java files in packages, you use a period (.) instead of the forward slash (/) to separate the package name from the file name:

Expand|Select|Wrap|Line Numbers
  1. javac tokenizer.TokenizerMain.java
(That is, I think so. I've had to actually go into the folder containing the .java files and compile them using javac *.java)

Second, if two classes are in the same package, they don't need to be imported to each other - they automatically have access to each other.
Nov 14 '08 #2

Expert 10K+
P: 11,448
First, when using javac to compile your .java files in packages, you use a period (.) instead of the forward slash (/) to separate the package name from the file name:

Expand|Select|Wrap|Line Numbers
  1. javac tokenizer.TokenizerMain.java
(That is, I think so. I've had to actually go into the folder containing the .java files and compile them using javac *.java)
No, javac compiles files so it uses those slashes; java runs classes so it uses
those dots. Compiling them all together is the way to go. Also type in 'javac' and
see what flags to use to point the compiler where the .class files can be found
and the .java source files can be found.

kind regards,

Jos
Nov 14 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
...right. This is why I should try this before I speak.
Nov 14 '08 #4

P: 2
Turns out I didn't even need the package stuff. I just ran javac <all my files> and it worked. Thanks for helping out you two.
Nov 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.