简体   繁体   English

设置我的Java环境。 JAR / CLASSPATH

[英]Setting up my Java Environment. Jar/CLASSPATH

So I started working with Java. 因此,我开始使用Java。 I am currently on Ubuntu Platform. 我目前在Ubuntu平台上。 I installed the jdk and it was working fine during the initial stages. 我安装了jdk,并且在初始阶段运行良好。 Then I turned to chapter 'ACCESS CONTROL' and there it started talking about the CLASSPATH and jars. 然后,我转到“访问控制”一章,在那里开始谈论CLASSPATH和jar。

Here's what I have done. 这就是我所做的。

On my Desktop I create a folder A and in that I create file Aa.java 在我的桌面上,我创建一个文件夹A然后在其中创建文件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 在桌面上,我还有另一个文件夹B和文件Bb.java

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. 现在,当我运行Bb.java时,出现错误,提示找不到Aa。

Ok, I understand that CLASSPATH is something, some variable that provides the location to search for other classes. 好的,我知道CLASSPATH是某种东西,它是一个变量,可提供搜索其他类的位置。 Now here are my questions 现在这是我的问题

  1. How do I know what my CLASSPATH is? 我怎么知道我的CLASSPATH是什么?
  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? 我的项目仅必须由CLASSPATH指向吗?
  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 ? 假设我的CLASSPATH指向桌面[即,我设置了CLASSPATH的值],现在对于我创建的每个文件,如果要导入其他类,那么我将import RELATIVE_ADDRESS_TO_DESKTOP吗?
  4. How do I set the value of CLASSPATH? 如何设置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? 我了解这是Class FIles的集合,但是我是否使用某些命令或...明确地创建了jar文件?它是如何工作的?

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 现在在文件夹Desktop / A中,我有

package A;

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

And inside folder Desktop/B, I have 在文件夹Desktop / B里面,我有

package B;

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

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

Now, I do javac Aa.java 现在,我做javac Aa.java

And javac Bb.java javac Bb.java

And when I do java Bb [ Inside folder Desktop/B To run the program, it says ] 而当我做java Bb [在文件夹Desktop / B内部运行该程序时,它说]

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. 线程“ main”中的异常java.lang.NoClassDefFoundError:Java的java.lang.ClassLoader.defineClass1(本机方法)处的Bb(错误名称:B / Bb)在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在java.net.URLClassLoader.findClass(URLClassLoader.java:205)的java.security.AccessController.doPrivileged(本机方法)处的$ 1.run(URLClassLoader.java:212)在java.lang.ClassLoader.loadClass(ClassLoader.java: 321),位于java.lang.ClassLoader.loadClass(ClassLoader.java:266)处sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:294)找不到主类:Bb。 Program will exit. 程序将会退出。

What is happening here? 这是怎么回事

EDIT 2 编辑2

When I do java B.Bb [Inside Folder B.Bb] It prints out AA . 当我做java B.Bb [Inside Folder B.Bb]它打印出AA

If someone can explain what is going on? 如果有人可以解释发生了什么?

1) How do I know what my CLASSPATH is? 1)我怎么知道我的CLASSPATH是什么?

It is a system environment variable. 它是系统环境变量。 On UNIX/Linux, you type "echo $CLASSPATH" to see what it is set to. 在UNIX / Linux上,键入“ echo $ CLASSPATH”以查看其设置。

2) In the first statement, I have written the complete address of the folder it is in. Even then it wont find it? 2)在第一个语句中,我已经写出了它所在文件夹的完整地址。 Does my project has to be pointed by CLASSPATH only? 我的项目仅必须由CLASSPATH指向吗?

That is a package name. 那是一个包名。 It tells Java where to look within the classpath. 它告诉Java在类路径中查找的位置。 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. 例如,假设我在文件系统/ user / local / com / stackoverflow / foo中有一个类。 Java needs the classpath set to /usr/local so it knows where to look for the class. Java需要将类路径设置为/ usr / local,以便知道在哪里查找该类。 Similarly, the file might be in the jar and the classpath says were to look in the jar. 同样,文件可能在jar中,而classpath表示要在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? 3)说我的CLASSPATH指向Desktop [即,我设置CLASSPATH的值],现在对于我创建的每个文件,如果我要导入其他类,那么我将导入RELATIVE_ADDRESS_TO_DESKTOP?

Yes. 是。 Your CLASSPATH points to the "root" of your package statement. 您的CLASSPATH指向package语句的“根”。

4) How do I set the value of CLASSPATH? 4)如何设置CLASSPATH的值?

export CLASSPATH="$CLASSPATH:textToAddToClasspath" export CLASSPATH =“ $ CLASSPATH:textToAddToClasspath”

5) What exactly is jar. 5)什么是罐子。 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? 我了解这是Class FIles的集合,但是我是否使用某些命令或...明确地创建了jar文件?它是如何工作的?

Yes, a jar is a collection of class files. 是的,罐子是类文件的集合。 You create it using "jar -cvf jarname.jar *.class" or the like 使用“ jar -cvf jarname.jar * .class”等创建它

Put Aa.java and Bb.java in the same folder. 将Aa.java和Bb.java放在同一文件夹中。 I believe that the JDK packages are set by default. 我相信JDK软件包是默认设置的。 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 这应该使您的程序正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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