i am currently trying to run a java file in the terminal inside the corresponding package, which i created in eclipse (so its not the default package). However, the problem began after i tried to compile my file with:
javac Übung13.java
This just produced the following output:
▄bung13.java:2: error: illegal character: '\u00bc'
package ├╝bung1;
Shortly, i found out, that the cause was the || character, which is equivalent to the "or"-operator in boolean algebra. I then tried :
javac -encoding UTF8 Übung13.java
There was no output, therefore i thought, that it compiled whithout any problems, and there was also an Übung13.class file inside the package, which also emphasized that i was right. But when i try to execute my java file, then it prints the following output:
java Übung13
Fehler: Hauptklasse Übung13 konnte nicht gefunden oder geladen werden
Ursache: java.lang.NoClassDefFoundError: übung1/Übung13 (wrong name: Übung13)
This implies that my package contains no class, but this cant be true, since i compiled the file successfully. This is the point where i also cant find any solutions. Help would be appreciated greatly.
Here is the source code of my java file in case that there are any mistakes, but the file ran without any errors in the eclipese IDE, so i highly doubt that.
// -*- coding: utf-8 -*-
package übung1;
public class Übung13 {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Das erste Kommandozeilenargument ist: " + args[0]);
} else {
System.out.println("Keine Kommandozeilenargumente vorhanden.");
}
/*
* args ist eine variable welche für das Terminal steht bzw. man kann sie als array betrachten
* welcher als index die zeilen angibt.
*/
System.out.println(args.length);
//Beispiel für eine Interaktion mit dem Terminal um die Summe zweier zahlen zu errechnen
try {
if (args.length == 0 || args.length == 1) {
}
} catch (NumberFormatException e) {
System.out.print("Falsche Eingabe");
};
}
}
Also my path to the java file:
eclipse-workspace\Übungen\src\übung1
1.
javac Übung13.java
javac -encoding UTF8 Übung13.java
java übung1.Übung13
java Übung13.java
First, I’ll point out something interesting: Java SE does not have a String class.
Java does have class named java.lang.String. When you compile code, the compiler automatically inserts
import java.lang.*;, which means wherever the word String appears in code, it is treated as shorthand forjava.lang.String.Why is this relevant? Because your program does not have a class named Übung13. Your program does have a class named
übung1.Übung13.So, the first thing to note is that it is never correct to just use “Übung13” by itself. The only correct way to run your program is #3 in your question:
java übung1.Übung13Why didn’t it work? Because your current directory does not contain an Übung13 class in the übung1 package.
Before running the program, change to the parent directory:
Then
java übung1.Übung13should work.A general rule is that if the command
dir übung1\Übung13.classdisplays a file, you can run the above java command. If that dir command does not list any files, you are in the wrong current directory, and Java will not recognize your class.The reason for this is that the Java classpath—the sequence of directories and .jar files which Java searches for classes—is always the current directory, unless you explicitly specify a classpath with the
-cpor--class-pathoption. If the current directory contains Übung13.java, then the current directory does not contain a übung1.Übung13 class.