Running Java code

java

#1

Hello, I’m trying to run a simple Java program but keep getting an error.
I am new to Cloud9, so don’t really know what I am doing.

Here is the code i am trying to run:

import java.util.Scanner;

class generator{
    static void generator(){
        Scanner in = new Scanner( System.in );
    
        System.out.println( "Enter your name,(with no spaces): " );
        String name = in.next();
    
        System.out.println( name );
    }
}

public class main{
    public static void main( String[] args ){
        generator name = new generator();
        name.generator();
    }
}`

and the error which keeps occurring:

Running main.java                                                                                                                                       
                                                                                                                                                    
Error: Could not find or load main class .home.ubuntu.workspace.Week0.Java.main                                                                         
                                                                                                                                                    
                                                                                                                                                    
Process exited with code: 123` 

Any help would be greatly appreciated.


#2

how do you run it to get that error?
You can use a runner like

{
    "script": [
        "ROOT=`pwd`",
        "printf \"\\033[01;34mCompiling all files in\\033[00m \\033[01;31m$ROOT\\033[00m\n\"",
        "OUT_DIR=\"$([ -f .bin ] && rm -r .bin; mkdir -p .bin ; echo .bin)\"",
        "FILES=\"$(find . -path './.c9*' -prune -o -print | grep '.*\\.java' | sed 's:^./::g' | tr '\n' ' ')\"",
        "MAIN_CLASS=\"$(echo $file | sed \"s:$ROOT/::g\" | sed 's:.java$::g' | sed 's:/:.:g')\"",
        "COMPILED_FILES=\"$(javac -d $OUT_DIR $FILES)\"",
        "java -classpath \"$OUT_DIR\" \"$MAIN_CLASS\" \"$args\""
    ],
    "env": {},
    "info": "\\033[01;34mRunning\\033[00m \\033[01;31m$file_name\\033[00m\n",
    "selector": "source.java"
}

#3

check your cwd in the terminal, make sure it’s the same as the main.java located if you have not declared any package


#4

How do you make a runner


#5

use run/run with/new runner menu item. see https://docs.c9.io/docs/custom-runners for details