0

私はJavaが初めてです..

次のような構造の例があります。

/folder/foo.java /folder/bar.java /folder/foobar.java

今、foobar.java を実行しようとしていますが、次の例外が発生します

Exception in thread "main" java.lang.ClassNotFoundException: /folder/foobar
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.apache.hadoop.util.RunJar.main(RunJar.java:149)

次に、次のようにして構成しようとしました

java -classpath  .  foobar 
Exception in thread "main" java.lang.NoClassDefFoundError: foobar
Caused by: java.lang.ClassNotFoundException: foobar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

手がかりはありますか?ありがとう

4

2 に答える 2

2

The default value of a classpath is ‘.’ , i.e. the current directory. The value of the classpath environment variable overrides this value. If the java command is issued with –cp or –classpath option, it overrides the default ‘.’ and classpath environment variable value.

Below is an example for setting a classpath during class execution C:>java -classpath "." com.abc.example.SayHello

As opposed to compiling where you need to give exact path, to run the class file, we need to follow the package structure.

This is due to the way the Classloader tries to resolve the class location by combining its package and class name. You must be on the package root location and issue the java command specifying the package structure.

C:>java com.abc.example.SayHello

Hello!!

于 2012-12-25T02:34:17.123 に答える
1

まず、Java ファイルをコンパイルする必要があります。

javac /folder/*.java

main()次に、関数を使用してクラスを実行できます。

java -cp . folder.foobar
于 2012-12-25T02:29:21.957 に答える