私は&のJavaでパッケージを使用する方法を学ぼうとしています。私は以下の "Hello World"プログラムを試しましたJavaパッケージ内のファイルを検索/実行するにはどうすればよいですか?
class helloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World");
}
}
このプログラムをホームディレクトリでコンパイルして実行すると、すべて問題ありません。しかし、私はサブディレクトリを作成するときに - ./testPackageをし、その中に次のファイル(hiEarth.java)を配置します。
package testPackage;
class hiEarth
{
public static void main (String[] args)
{
System.out.println("Hi Earth");
}
}
私は、一見それをコンパイルすることができますが、それを実行することはできません。
[email protected]:~/Projects/JavaProjects/helloWorld/testPackage$ javac hiEarth.java
[email protected]:~/Projects/JavaProjects/helloWorld/testPackage$ java hiEarth
Exception in thread "main" java.lang.NoClassDefFoundError: hiEarth (wrong name: testPackage/hiEarth)
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: hiEarth. Program will exit.
私は「ls
」を実行するとき、私は私が見に期待するものを参照してください。
[email protected]:~/Projects/JavaProjects/helloWorld/testPackage$ ls
hiEarth.class hiEarth.java
は、私は現在のディレクトリにクラスを参照してくださいするには、Javaを得ることができないのはなぜ?
私は1つのディレクトリの上に移動した場合:
[email protected]:~/Projects/JavaProjects/helloWorld/testPackage$ cd ..
[email protected]:~/Projects/JavaProjects/helloWorld$ java testPackage/hiEarth
物事がうまく動作。これはクラスパスの問題だと思っていましたが、
[email protected]:~/Projects/JavaProjects/helloWorld/testPackage$ java -cp . hiEarth
のいずれも動作しません。私はここで何を理解していないのですか?
おかげで....
ありがとうございました。実際、helloWorldからjava testPackage.hiEarthはうまく動作します。しかし、私が./testPackageに移動すると、java -cp .. testPackage.hiEarthを使用する必要があります。また、NBのおかげで、私はそれを念頭に置いておきます。 – user1245262