简体   繁体   English

关于Linux上Java的classpath设置

[英]about Java's classpath setting on Linux

everyone. 大家。

I used openjdk-7 on arch linux. 我在arch linux上使用过openjdk-7。 I started to learn Java recently, and encountered such a problem: 我最近开始学习Java,遇到了这样的问题:

I created a file at /home/hqwrong/Code/java/mew/Mouth.java: 我在/home/hqwrong/Code/java/mew/Mouth.java中创建了一个文件:

package mew;

public class Mouth{
   public static void main(String argv[]){
       pickle.Say s = new pickle.Say();
  }
}

and another one at /home/hqwrong/Code/java/pickle/Say.java : 另一个位于/home/hqwrong/Code/java/pickle/Say.java:

package pickle;

public class Say{
   public Say(){
      System.out.println("Say");
   }
}

I compiled Say.java to Say.class,using: 我使用以下方法将Say.java编译为Say.class:

$ cd /home/hqwrong/Code/java/pickle
$ javac Say.java

which is successful. 这是成功的。

I compiled Mouth.java ,using: 我使用以下命令编译了Mouth.java:

$ cd ../mew
$ export CLASSPATH=.:/home/hqwrong/Code/java/
$ javac Say.java

no error message. 没有错误消息。

But after I type: 但是在我输入之后:

$ java Say $ java说

I got: 我有:

Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.mew
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:649)
at java.lang.ClassLoader.defineClass(ClassLoader.java:785)
 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:472)

It's same when I use: 使用时是相同的:

$ java -cp $CLASSPATH Say

I need your help,please? 我需要你的帮助吗?

Since there is no good answer yet, I'll post mine. 既然没有好的答案,我将发布我的信息。

First, you should really have a separate folder for your classes and your sources. 首先,您确实应该为您的课程和您的资源有一个单独的文件夹。 I suggest using java/src for your sources, and java/classes for your classes. 我建议使用java/src为你的源代码,并java/classes为你的类。 Since the classes are stored in the classes folder, this is the one that should be in the classpath. 由于这些类存储在classes文件夹中,因此这是应该放在类路径中的类。

The folder tree of your sources should then match your package tree. 源文件的文件夹树应与程序包树匹配。 This means that the class mew.Mouth must contain the line package mew , be defined in the Mouth.java file, in the java/src/mew folder. 这意味着类mew.Mouth必须包含行package mew ,该行package mew是在Mouth.java文件的java/src/mew文件夹中定义的。

To compile your classes, put you in the java/src directory, and use the following command: 要编译您的类,请将您放入java/src目录,并使用以下命令:

javac -d ../classes mew/Mouth.java pickle/Say.java

The compiler will automatically generate the folder structure matching the package structure in the classes directory. 编译器将自动生成与classes目录中的包结构匹配的文件夹结构。 If you make structural modifications in your source tree, just remove everything in the classes folder, and recompile everything. 如果您在源代码树中进行结构修改,只需删除classes文件夹中的所有内容,然后重新编译所有内容。

To run your classes, you must refer to their fully qualified name. 要运行您的类,您必须引用其完全限定名称。 And the folder containing your package tree (the java/classes folder) must be in the classpath. 并且包含软件包树的文件夹( java/classes文件夹)必须位于类路径中。 Once this is done, from everywhere, you can use 完成此操作后,您可以在任何地方使用

java mew.Mouth

Note that, as you have discovered, the java and javax packages are reserved. 请注意,您已经发现,保留了javajavax包。 You can't use them for your own classes. 您不能将它们用于自己的课程。

Please try this, 请尝试一下

open your root folder, Go to view Menu & tick , view hidden files. 打开您的根文件夹,转到查看菜单并打勾,查看隐藏的文件。 Now It will display a file called ".bashrc". 现在它将显示一个名为“ .bashrc”的文件。 open this file & write down following lines of code, 打开此文件并写下以下代码行,

PATH=$PATH:/opt/jdk1.6.0_21/bin
export PATH
JAVA_HOME=/opt/jdk1.6.0_21
export JAVA_HOME

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

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