问题和练习的答案:平台环境

Questions

问题 1. 程序员将安装一个包含在.jar 文件中的新库。为了从他的代码访问该库,他将 CLASSPATH 环境变量设置为指向新的.jar 文件。现在,他发现在try启动简单应用程序时收到错误消息:

java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello

在这种情况下,Hello类被编译为当前目录中的.class 文件-但是java命令似乎找不到它。怎么了

答案 1. 仅当类出现在 Classpath 中时,才会找到该类。默认情况下,Classpath 由当前目录组成。如果设置了 CLASSPATH 环境变量,并且不包含当前目录,则启动器将无法在当前目录中找到类。解决方案是将 CLASSPATH 变量更改为包括当前目录。例如,如果 CLASSPATH 值为c:\java\newLibrary.jar(Windows)或/home/me/newLibrary.jar(UNIX 或 Linux),则需要将其更改为.;c:\java\newLibrary.jar.:/home/me/newLibrary.jar

Exercises

Exercise 1.

编写具有以下功能的应用程序PersistentEcho

  • 如果PersistentEcho与命令行参数一起运行,它将打印出这些参数。它还会将打印出的字符串 保存到属性中,并将属性保存到名为PersistentEcho.txt的文件中

  • 如果PersistentEcho在没有命令行参数的情况下运行,它将查找名为 PERSISTENTECHO 的环境变量。如果该变量存在,则PersistentEcho打印出它的值,并以与处理命令行参数相同的方式保存该值。

  • 如果PersistentEcho在没有命令行参数的情况下运行,并且未定义 PERSISTENTECHO 环境变量,它将从PersistentEcho.txt检索属性值并将其打印出来。

Answer 1.

import java.util.Properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PersistentEcho {
    public static void main (String[] args) {
        String argString = "";
        boolean notProperty = true;

        // Are there arguments? 
        // If so retrieve them.
        if (args.length > 0) {
            for (String arg: args) {
                argString += arg + " ";
            }
            argString = argString.trim();
        }
        // No arguments, is there
        // an environment variable?
        // If so, //retrieve it.
        else if ((argString = System.getenv("PERSISTENTECHO")) != null) {}
        // No environment variable
        // either. Retrieve property value.
        else {
            notProperty = false;
            // Set argString to null.
            // If it's still null after
            // we exit the try block,
            // we've failed to retrieve
            // the property value.
            argString = null;
            FileInputStream fileInputStream = null;
            try {
                fileInputStream =
                    new FileInputStream("PersistentEcho.txt");
                Properties inProperties
                    = new Properties();
                inProperties.load(fileInputStream);
                argString = inProperties.getProperty("argString");
            } catch (IOException e) {
                System.err.println("Can't read property file.");
                System.exit(1);
            } finally {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch(IOException e) {};
                }
            }
        }
        if (argString == null) {
            System.err.println("Couldn't find argString property");
            System.exit(1);
        }

        // Somehow, we got the
        // value. Echo it already!
        System.out.println(argString);

        // If we didn't retrieve the
        // value from the property,
        // save it //in the property.
        if (notProperty) {
            Properties outProperties =
                new Properties();
            outProperties.setProperty("argString",
                                      argString);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream =
                    new FileOutputStream("PersistentEcho.txt");
                outProperties.store(fileOutputStream,
                        "PersistentEcho properties");
            } catch (IOException e) {}
            finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch(IOException e) {};
                }
            }
        }
    }
}