skip to Main Content

Whenever I run a java program, both class name and filename have to be same, because the command to compile and run java in vscode terminal is:

cd "c:UsersuserDesktopJava" ; if ($?) { javac MyProgram.java } ; if ($?) { java MyProgram }

and when my class name is different from filename I have to edit the command to ...; if ($?) { java ActualClassname } in terminal every time I run the code.

So how do I change this command for running and compiling java program in vscode?

I just have to change it a little bit from:

cd "c:UsersuserDesktopJava" ; if ($?) { javac MyProgram.java } ; if ($?) { java MyProgram }

to:

cd "c:UsersuserDesktopJava" ; if ($?) { javac MyProgram.java } ; if ($?) { java MyProgram.java }

Adding .java in the end seems to solve my problem whenever I have different class name from filename.

Reason for asking this is some names for class were giving me errors
for example:

//reverse-string.java
import java.util.Scanner;
public class reverse-string //error:"invalid modifiers"
{
    public static void main(String[] args) {
        String str, rev="";
        char ch;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a String:");
        str = in.next();
        System.out.println("String = "+str);
        for (int i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            rev = ch + rev;
        }
        System.out.println("Reverse String = "+rev);
        in.close();
    }
}

So I used different names for class and file that’s why had to remove public from class:

//reverse-string.java
import java.util.Scanner;
class ReverseString
{
    public static void main(String[] args) {
        String str, rev="";
        char ch;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter a String:");
        str = in.next();
        System.out.println("String = "+str);
        for (int i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            rev = ch + rev;
        }
        System.out.println("Reverse String = "+rev);
        in.close();
    }
}

and that worked both in vscode and command line using javac reverse-string.java and java ReverseString

After searching in the internet (chatgpt) I found out that changing the task.json in .vscode directory would change the command that is required to run the program. But I don’t know how do I change and even how do I open this task.json only for Java? It is very confusing.

2

Answers


  1. Java requires that the .java file name matches the name of the public class defined in that file. VS Code is simply helping you follow this rule. You shouldn’t try to get around it.

    The real problem here is that reverse-string is not a valid class name because - is not allowed in any identifier in Java. Instead, you should name the class ReverseString and the file as ReverseString.java. While the capital first letter is not strictly required by Java, it is a common convention to differentiate class names easily from other names, such as methods and variables.

    Alternatively, Java won’t allow you to name the file reverse-string.java with a class named ReverseString. I recommend using the command line directly with something like javac reverse-string.java to try it. Even if this works, running java ReverseString won’t work.

    Login or Signup to reply.
  2. You should be using Code Runner to run the script. Add this setting

        "code-runner.customCommand": "cd $dir && javac $fileName && java $fileName",
    

    Then select RUn Custom Command in the command panel or use the shortcut key Ctrl+Alt+K to execute the script using the command you defined.

    enter image description here

    Or you can also modify the execution command of the java language in the code-runner.executorMap setting:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search