Alex Rivera | Logout

How to run a NetBeans project in command prompt?

Asked 2009-03-09T16:15:28.760
18

My professor asked us to create a Java program that would be able to run in command prompt but could also be opened using NetBeans.

The program is about using the different types of sorting (specifically Selection, Insertion, Exchange, Quick, and Heap sorting). our professor specifically told us to use object oriented programming in Java, and that she wants to see a main class plus the different classes that would do the sorting.

I tried to write the program in NetBeans — thinking that later I could simply run the program in cmd using javac.

In cmd, I typed the path where my NetBeans project was saved and I tried to compile the files using javac. but it says that "'javac' is not recognized as an internal or external command, operable program or batch file."

So I tried to save the files in sun>sdk>jdk>bin, and from there I tried to compile the files, and it was fine. The problem sets in when I tried to run them.

Here's how I tried to compile the files:

javac Main.java
      Sortchoice.java
      Selection.java
      SelectionSort.java
      Insertion.java
      InsertionSort.java
      Exchange.java
      ExchangeSort.java

(I havent finished the syntax for the next two sorting.)

Here's how I tried to run the files in cmd:

java Main Sortchoice Selection SelectionSort Insertion InsertionSort Exchange ExchangeSort

and cmd says:

exception in thread "main" java.lang.NoClassDefFoundError: main (wring name: myjava/Main)
at java.lang.ClassLoader.defineClass1(Nativ... Method)"
at java.lang.ClassLoader.defineClass(ClassL...
at java.security.SecureClassLoader.defineCl...
at java.net.URLClassLoader.defineClass(URLC...
at java.net.URLClassLoader.access$000(URLCl...
at java.net.URLClassLoader$1.run(URLClassLo...
at java.security.AccessController.doPrivile... Method)
at java.net.URLClassLoader.findClass(URLCla...
at java.lang.ClassLoader.loadClass(C
Edit
Report

1 Answer

14

The best way to do it is this:

1) create a directory with src\ and tests\ in it (the tests is optional if you are not using JUnit).

2) assuming you have package myjava; at the top of your files (and make sure that this is what your prof wants, it becomes a pain to mark things if they are not in the right place), make a src\myjava directory (and if you are doing JUnit a tests\myjava directory).

3) copy your files into the src\myjava directory

4) delete your NetBeans project and recreate it as a new on with exising sources. When you are setting up the src (and optional test) directories add the src\ (and optionally the tests\) directory. DO NOT add the src\myjava directory or it won't work in NetBeans.

5) make a directory called classes\ (so you you have src\, classes\, and maybe \tests all in the same place)

6) on the command line type javac -d classes -cp classes src/myjava/*.java

  • -d tells the compiler where to put the .class files
  • -cp tells the compiler where to look for classfiles
  • src/myjava/*.java tells it to compile all of the .java files in src/myjava

7) run it via java -cp classes myjava.Main

  • -cp classes tells it to look in the classes directory for the .class files
  • myjava.Main is the name of the class to run
answered 2009-03-09T16:52:06.160

Your Answer