0

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
4
  • read through your question and there are several things that dont add up. \u00bc is the unicode character for ¼ also your error message is complaining about the Ü in classname. Its hard to get a piccture of this but you can change the name of your compiled jar to .zipand extract its contents to look at what the directory structure looks like inside the jar-file. But its very hard to help since you have not supplied any simple instructions how to reproduce. Commented Dec 17, 2023 at 10:57
  • java -XshowSettings:all 2>&1 | findstr "encoding" Commented Dec 17, 2023 at 13:53
  • the class is declared to be in the package called übung1 - so it must be executed as java übung1.Übung13 but from the folder that contains übung1, that is, from eclipse-workspace\Übungen\src assuming that Übung13.class was created in eclipse-workspace\Übungen\src\übung1 (or eclipse-workspace\Übungen\src must be added to the CLASSPATH: java -cp eclipse-workspace\Übungen\src übung1.Übung13`) -- anyway strongly discouraged to use Umlaute (or similar) in package or class names - but note that eclipse usually is configured to use a different folder for generated .class files Commented Dec 17, 2023 at 13:54
  • Personally I wouldn't use umlauts or other diacritics in package or class names as they are not very portable. But your code should work per the comments of @user85421 Commented Dec 17, 2023 at 13:57

1 Answer 1

1

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 for java.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.Übung13

Why 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:

cd ..

Then java übung1.Übung13 should work.

A general rule is that if the command dir übung1\Übung13.class displays 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 -cp or --class-path option. If the current directory contains Übung13.java, then the current directory does not contain a übung1.Übung13 class.

Sign up to request clarification or add additional context in comments.

10 Comments

A general rule is that if the command dir übung1\Übung13.class .... I don't want to nitpick but that class might be in the default package but its file just happen to be in that directory.
@g00se java.lang.NoClassDefFoundError: übung1/Übung13 (wrong name: Übung13) suggests otherwise.
Ah no, I'm making a different point: that you can't necessarily infer that the class Übung13 is in package übung1 merely from the name of its parent directory. You were referring to "A general rule" Your point is probably sound for a specific instance
Your answer is highly interesting, but how can a Ü be a valid character in a Java class name? I found the following definition: "A class name is an identifier—a series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces". Hence, whether or not an Ü (or, say, a ) is considered a letter, is at best dependent on the locale, and such a program would face portability issues.
@user1934428 You are correct. Whether Java treats a character as a letter will depend on correctly recognizing that letter, which means the Java compiler needs to know the encoding of the source file. At runtime, it shouldn’t matter; if the operating system sees the filename correctly, Java most likely will, too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.