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 di
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
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.
To run, you just need
java Main
instead of putting every class on the command line.
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?
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 classfilessrc/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 filesmyjava.Main
is the name of the class to runIf 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"
Right click My Computer>properties>advanced>environment variables...add the bin directory in jdk to the Path variable example: Variable Name: path Variable value: C:\Program Files\Java\jdk1.6.0_16\bin
java and javac should now work from any directory in the command prompt