Compile and run Java file without IDE: Error, main class not found

0

In my programming class we were asked to make a simple program and compile it, run it and create a .jar from the CMD in windows (WITHOUT IDE); which I still do not know how to do, because I always used Netbeans.

The file path is:

  

C: \ JavaWorks \ RFCJava \ GetRFC.java

The file is as follows:

package getrfc;
import javax.swing.JOptionPane;
public class GetRFC {
    //contenido de la clase
    }
    public static void main(String[] args) {
        pedirDatos();
    }
}

I have done the following:

  

C: > cd C: \ JavaWorks \ RFCJava

     

C: \ JavaWorks \ RFCJava > javac GetRFC.java

A .class file has been generated in the same directory, normal. When trying to run with "java", I get it:

  

C: \ JavaWorks \ RFCJava > java GetRFC

     

Error: The main GetRFC class was not found or loaded

But the GetRFC.class class is right there, so it does exist. We are starting programming, so maybe I omitted something simple. Any help is welcome; since I also do not know how to do the .jar and as far as I have researched, I need first that the java GetRFC works. As an additional note, the directory only contains the .java and .class file; nothing more.

    
asked by Hydrako 11.05.2017 в 03:00
source

1 answer

1

Consider that the packages are a structure of folders that serve to organize your code. If your class starts with the following line:

package getrfc;

It means that your .java file must be inside a getrfc folder. Your folder and file structure should look like this:

- C:/
  - algunaCarpetaParaTusProyectos/
    - getrfc
      + GetRFC.java

Then, by command line (cmd in Windows), you access the folder where your file is located. Example (the entry of commands is denoted by > ):

> cd C:\algunaCarpetaParaTusProyectos
> dir
  getrfc
> cd getrfc
> dir
  GetRFC.java

Next, you must compile the file with javac . To execute it, you must upload through the folders until you reach at the same level as the root of the packages and use the full name of the class. The full name is the name of the folder (or folders, depending on the package where you are) and the name of the file:

# ya estamos dentro de C:\algunaCarpetaParaTusProyectos\getrfc
> javac GetRFC.java
# asumiendo que no hay errores de compilación
> cd ..
> java -cp . getrfc.GetRFC
# salida de tu programa Java
    
answered by 11.05.2017 / 04:21
source