How to run a NetBeans project in command prompt?

2019-01-22 21:02发布

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(ClassLoa...
at sun.misc.Launcher&AppClassLoader.loadCla...
at java.lang.ClasLoader.loadClass(ClassLoad...
at java.lang.ClassLoader.loadClassInternal(...

What should I do? (Sorry for my kilometric-long explanation. I just wanted to put in as many details as possible.)

I would also like to emphasize that I'm just a beginner in Java programming.

7条回答
甜甜的少女心
2楼-- · 2019-01-22 21:23

Create a folder somewhere, say C:\myjava. Copy your .java source files from wherever netbeans saves them to your C:\java for example. Open each of these .java files you have just pasted and delete the line that states the package e.g package myjava. Compile using javac e.g. javac c:\myjava\MyJava.java. Then run it as in, java c:\myjava\MyJava

查看更多
萌系小妹纸
3楼-- · 2019-01-22 21:27
  1. It would be better to add javac to your PATH environment variable instead of putting the .java files into the same directory with it. It'll get awfully crowded in there.
  2. To run, you just need

    java Main

    instead of putting every class on the command line.

  3. Did you declare a package in your .java files? Like,

    package myjava;

    ? If so, the command string must be

    java myjava.Main

Does that answer your questions?

查看更多
Explosion°爆炸
4楼-- · 2019-01-22 21:30

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
查看更多
你好瞎i
5楼-- · 2019-01-22 21:31

Follow these steps (code examples are just examples, you will need to tweak some for your setup):

  1. Set your JAVA_HOME to the directory where the JDK is installed.

    set JAVA_HOME="c:\Program Files\Java\jre1.6.0_12"

  2. Set your PATH to include the bin directory in JAVA_HOME.

    set PATH=%PATH%:%JAVA_HOME%\bin

  3. Change to the root directory of your source code. If you have declared your code to be in packages, this is the root directory of your package structure. If not, this is the directory that contains your .java files:

    cd c:\My\Source\Directory

  4. Execute javac with your Java files as the argument:

    javac Class1.java Class2.java

  5. (Assuming everything compiles correctly) Execute java with the name of the class containing your main method as the argument (if you included package declarations, then your class name is fully-qualified, meaning it should include the package name before the class name, with a '.' separating the package name from the class name):

    java Main

查看更多
唯我独甜
6楼-- · 2019-01-22 21:33

If you click build inside NetBeans, it should give you (in your compiler output) a message like, "To run this application from the command line without Ant, try: java -jar yourPathToRun"

查看更多
beautiful°
7楼-- · 2019-01-22 21:39

The easiest way to do this is to run the .jar file in the dist folder under your project's main folder. Make sure the path to the jdk bin folder in your computer's System, Environmental Variables is set to find the usual: jar.exe, java.exe, javac.exe, etc, (see other posts in this thread if you need instructions on doing that).

1) In Netbeans do a clean build, press F11 or click on the Run menu and click on Build Project. Click through to get past all the prompts, you need a clean build to do this.

2) in the command prompt navigate to your project's dist folder

3) on the command line type in and run: java -jar yourMainFile.jar

No creating separate folders, no copying files, no multiple files to include on the command line argument. But you will have to re-build your project each time you change your code before running it on the command line.

查看更多
登录 后发表回答