I have written a simple package program:
//A simple package
package MyPack
class Balance
{
String name;
doubl
Try to create the folder named MyPack
and move .class files in it or use following command line to compile the java code and it moves the .class files into MyPack
folder.
javac YourFileName.java -d .
and run using,
java MyPack.AccountBalance
Have a look at javac tool
From the tool doc : By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d (see Options, below).
and package tutorial.
Best is to compile and run the classes from outside the packages :
First you compile with javac :
$javac MyPack/AccountBalance.java
this will create a new file in the MyPack folder called AccountBalance.class
then you can run it :
$java MyPack.AccountBalance
By the way: it is discouraged to have package names start with a capital.
When you are trying to compile the java class, use the '-d' option (destination) to specify where the .class files should be located.
javac -d "classes" AccountBalance.java
and when you run your program, make sure that same folder is included in your class path:
java -classpath "classes" MyPack.AccountBalance
If you're frequently compiling and running via javac
and java
commands then use
javac MyJavaClass.java && java MyJavaClass
This'll compile the the class then run the class you just compiled.
Note: Replace &&
with ;
if using Windows powershell.