0

In my pc I'm always having a problem with running my java code in cmd . It shows "error loading main class" (though I commented out project name, set the classpath,set the PATH variable, but still no change) all the time. My teacher told me to create a new file and store my .java files there and edit it with notepad++ and create a .bat format file.

But I'm not sure about the whole process because I tried to do that it showed error again "error loading main class" (may be I missed some steps). Can anyone please help me with this? Any help would be really appreciated.

0ndre_
  • 3,577
  • 6
  • 26
  • 44
IAmBlake
  • 457
  • 6
  • 21
  • 3
    what is the error? Be specific and clear about it. – Shadab K Nov 21 '16 at 16:28
  • 1
    Also, please show the command you used. – Jorn Vernee Nov 21 '16 at 16:28
  • Maybe your teacher should teach you how to use an IDE. You'll use an IDE once you're employed. Companies are not looking for developers how write code in Notepad. An IDE has many benefits. – Thomas Weller Nov 21 '16 at 16:30
  • i'm trying to build a chat messenger with one server multiple client so i need cmd to for passing messages to different client at the same time, can i do it (running same file several times at the same time)by passing arguments in netbeans or intellij? – IAmBlake Nov 21 '16 at 16:35

1 Answers1

0

Let's assume you have this directory layout:

myproject/
    src/
        mypackage/
            MyClass1.java
            MyClass2.java
    target/

... and let's also asume that you open a shell with myproject as the working directory.

You should compile your source code and store the produced .class files into the target dir. For example:

javac -d target src\mypackage\*.java

And finally, to execute the main method in MyClass1, you should execute this:

java -classpath target mypackage.MyClass1 <arguments...>

Of corse, if you needed more third-party libraries, then you must add them to the classpath:

In Windows:

java -classpath target;library1.jar;library2.jar mypackage.MyClass1 <arguments...>

In Unix:

java -classpath target:library1.jar:library2.jar mypackage.MyClass1 <arguments...>
Little Santi
  • 8,563
  • 2
  • 18
  • 46