简体   繁体   中英

Setting up my Java Environment. Jar/CLASSPATH

So I started working with Java. I am currently on Ubuntu Platform. I installed the jdk and it was working fine during the initial stages. Then I turned to chapter 'ACCESS CONTROL' and there it started talking about the CLASSPATH and jars.

Here's what I have done.

On my Desktop I create a folder A and in that I create file Aa.java

package home.kraken.Desktop.A;

class Aa{
    public doSome(){
        System.out.println("AA");
    }
}

On Desktop I have another folder B and file Bb.java in that

package home.kraken.Desktop.B;

import  home.kraken.Desktop.A.*;
class Bb{
    public static void main(String[] args){

        Aa a = new Aa();
        a.doSome();
    }
}

Now, when I run Bb.java I get error that it can not find Aa.

Ok, I understand that CLASSPATH is something, some variable that provides the location to search for other classes. Now here are my questions

  1. How do I know what my CLASSPATH is?
  2. In the first statement, I have written the complete address of the folder it is in. Even then it wont find it? Does my project has to be pointed by CLASSPATH only?
  3. Say my CLASSPATH points to Desktop [ie I set the value of CLASSPATH], now for every file I create, if I want to import some other class, then I will import RELATIVE_ADDRESS_TO_DESKTOP ?
  4. How do I set the value of CLASSPATH?
  5. What exactly is jar. I understand it is the collection of Class FIles but do I explicitly make a jar file using some command or ...How does it work?

Thanks.

EDIT

So here's what I did

My classpath originally was empty so I did

export CLASSPATH="$CLASSPATH:/home/kraken/Desktop"

Now Inside folder Desktop/A, I have

package A;

public class Aa{
    public void doSome(){
        System.out.println("AA");
    }
}

And inside folder Desktop/B, I have

package B;

import  A.*;
class Bb{
    public static void main(String[] args){

        Aa a = new Aa();
        a.doSome();
    }
}

Now, I do javac Aa.java

And javac Bb.java

And when I do java Bb [ Inside folder Desktop/B To run the program, it says ]

Exception in thread "main" java.lang.NoClassDefFoundError: Bb (wrong name: B/Bb) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:634) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) at java.net.URLClassLoader.access$000(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:212) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:321) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:266) Could not find the main class: Bb. Program will exit.

What is happening here?

EDIT 2

When I do java B.Bb [Inside Folder B.Bb] It prints out AA .

If someone can explain what is going on?

1) How do I know what my CLASSPATH is?

It is a system environment variable. On UNIX/Linux, you type "echo $CLASSPATH" to see what it is set to.

2) In the first statement, I have written the complete address of the folder it is in. Even then it wont find it? Does my project has to be pointed by CLASSPATH only?

That is a package name. It tells Java where to look within the classpath. Normally, the package name doesn't match the absolute path of the file. For example, suppose I have a class located on my file system /user/local/com/stackoverflow/foo. Java needs the classpath set to /usr/local so it knows where to look for the class. Similarly, the file might be in the jar and the classpath says were to look in the jar.

3) Say my CLASSPATH points to Desktop [ie I set the value of CLASSPATH], now for every file I create, if I want to import some other class, then I will import RELATIVE_ADDRESS_TO_DESKTOP?

Yes. Your CLASSPATH points to the "root" of your package statement.

4) How do I set the value of CLASSPATH?

export CLASSPATH="$CLASSPATH:textToAddToClasspath"

5) What exactly is jar. I understand it is the collection of Class FIles but do I explicitly make a jar file using some command or ...How does it work?

Yes, a jar is a collection of class files. You create it using "jar -cvf jarname.jar *.class" or the like

Put Aa.java and Bb.java in the same folder. I believe that the JDK packages are set by default. So, if you put the two files in the same folder, all you have to do is set the classpath to the folder that you have everything in.

java -classpath /home/user/myprogram org.mypackage.AA.java

or whatever your file structure is ^^

this should get your program to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM