Java under UNIX v/s Windows-NT

Hi

Is there any special consideration for a Java Program

to work under UNIX.

My problem is as follows,

I have a Java program say "JavaProg.java".

I create its class file under Windows-NT i.e.

"JavaProg.class" after compiling the java code using

the command "javac JavaProg.java".

Everythings perfect till here. The class file generates

output as expected under Windows-NT

Problem starts when I transfer that class file to UNIX.

With the initial promise of Java as "Write Once,

Run Everywhere" I just take the class file and put

it in my directory under UNIX.

i.e. home/myfiles/javacodes/programs

I set the PATH as "/home/java/bin" & CLASSPATH variable

as "/home/java/lib" since these are the directories where

the java.exe executable and the library files are existing.

Then when I execute

"java JavaProg" from my homedirectory of home/myfiles/javacodes

/programs it gives the following error,

Can't find the class file JavaProg

So I try out various other options like

"java JavaProg.class" (Specifying the .class extension)

on which it says

Invalid class name JavaProg.class

So I try,

"java /home/myfiles/javacodes/programs/JavaProg.class"

on which it says

Error finding class JavaProg.class : Wrong Name

=======================================================

Under Windows-NT I just have to do the following 4 steps,

1. set PATH=.;c:\jdk1.1.8\bin

2. set CLASSPATH=.;c:\jdk1.1.8\lib

3. Run the following command,

"javac JavaProg.java"

4. Run the following command,

"java JavaProg"

=======================================================

My Questions,

1. Are there any special considerations when class files

are placed under UNIX.

2. Does the above for Windows-NT apply exactly the same manner

in UNIX too. Or is there some additional thing to be done.

3. Are there any environment variables to be set under UNIX

which I might be missing.

4. How do you set PATH & CLASSPATH in UNIX.

5. Does the Java code have to be recompiled under UNIX.

I personally feel "NO" since Java promises "Write Once,

Run Everywhere" theme. So the same class file should even

work under UNIX even though it was intially compiled using

Windows-NT.

Can someone please respond as early as possible as I am

close to the project deadline. Any help appreciated. Thanks

in advance to any help, insights, views, ideas anyone can

throw on this subject.

Thanks,

Jatin

[2771 byte] By [Jatin2000] at [2007-9-26 1:33:52]
# 1

1. Are there any special considerations when class files

are placed under UNIX.

Same on any platform.

2. Does the above for Windows-NT apply exactly the same manner

in UNIX too. Or is there some additional thing to be done.

Yes. For your case, you just mis-understood the CLASSPATH variable. The simpliest to solve your problem is change your CLASSPATH to just a single dot like this: CLASSPATH=.

3. Are there any environment variables to be set under UNIX

which I might be missing.

no, same as NT

4. How do you set PATH & CLASSPATH in UNIX.

in your startup script such as .profile, or do

set CLASSPTH=.

export CLASSPATH

5. Does the Java code have to be recompiled under UNIX.

I personally feel "NO" since Java promises "Write Once,

Run Everywhere" theme. So the same class file should even

work under UNIX even though it was intially compiled using

Windows-NT.

no, it does NOT need to be recompiled.

--lichu

lichudang at 2007-6-29 2:16:14 > top of Java-index,Developer Tools,Java Compiler...
# 2

CLASSPATH variable should not point to the java lib directory. In your NT and Unix case, you can safely remove the c:\jdk1.1.8\lib or /home/java/lib from your CLASSPATH. It should instead point to your classes' package root. Since the class you wrote does not use a package name, your CLASSPATH should point to the directory of your class located home/myfiles/javacodes/programs. You can then run your java class in any path. You can also set your CLASSPATH to a dot, then you can only run your class under the directory of your class.

In fact, there are some considerations to move Java classes from one platform to another. Such as Unix's classpath variable is seperated by colon :, and Windows' classpath variable is seperatred by semicolon ;. Just keep this in mind.

--lichu

lichudang at 2007-6-29 2:16:14 > top of Java-index,Developer Tools,Java Compiler...
# 3

Hi

Thanks for your help,

What do yo

"It should instead point to your classes' package root. Since the class you wrote does not use a package name, your CLASSPATH should point to the directory of your class located home/myfiles/javacodes/programs

u exactly mean by saying that"

My Java program is an independent standalone

application with "main" method. It is not a part of any package. It however, uses std. Java packages such as

import java.io.*;

import java.lang.Object;

import java.util.*;

Do you mean the above. So should my class path be

something like the below,

CLASSPATH=.:/home/opt/java/lib

Can you please respond.

Thanks for your help in advance.

Jatin

Jatin2000 at 2007-6-29 2:16:14 > top of Java-index,Developer Tools,Java Compiler...
# 4

Sorry, some typos.

Writing again.

Hi

Thanks for your help,

What do you mean exactly by the following,

"It should instead point to your classes' package root. Since the class you wrote does not use a package name, your CLASSPATH should point to the directory of your class located home/myfiles/javacodes/programs

u exactly mean by saying that"

My Java program is an independent standalone

application with "main" method. It is not a part of any package. It however, uses std. Java packages such as

import java.io.*;

import java.lang.Object;

import java.util.*;

Do you mean the above. So should my class path be

something like the below,

CLASSPATH=.:/home/opt/java/lib

Can you please respond.

Thanks for your help in advance.

Jatin

Jatin2000 at 2007-6-29 2:16:14 > top of Java-index,Developer Tools,Java Compiler...
# 5

Hi, Jatin

A typical java class should have a package statement before your import statements, for example:package com.myCompanyName.myPackageName;

import java.io.*;

import java.util.*;

public class Abc {

...

}

Package effectively distinguish your classes from anyone else's classes. You will find the package keyword very useful and essential in Java after developing a dozen classes and working with others' libraries.

In the above example, the class Abc will be compiled into a Abc.class file. Now where does this file go is closely related to your package declaration. This Abc.class has to reside in a directory called myPackageName, the this myPackageName directory has to reside in a directory called myCompanyName, and myCompanyName directory has to be in a directory call com. Finally, where do you put this com directory? The answer is any place you want as long as your CLASSPATH variable is pointing to that directory. So let's say your class Abc is now in

/home/myJavaPrograms/com/myCompanyName/myPackageName/Abc.class

Then your classpath should have

/home/myJavaPrograms

This is what we call it "package root".

If your class does not have any package declaration, then your class belongs to the root package and your classpath should point to the class's immediate parent directory.

--lichu

lichudang at 2007-6-29 2:16:14 > top of Java-index,Developer Tools,Java Compiler...