I am not sure what you are doing wrong so I will just show you how it can be done.
Lets say you have directories and files
[myProject]
|
+--[src]
| |
| +--[com]
| |
| +--[DatingService]
| |
| +-- Main.java
|
+--[classes]
and your Main.java file looks something like
package com.DatingService;
class c{
private int i;
public void setI(int i){
this.i=i;
}
public int getI(){
return this.i;
}
}
public class Main{
public static void main(String[] args){
c myCVariable = new c();
myCVariable.setI(10);
System.out.println(myCVariable.getI());
}
}
In terminal you need to go to myProject
directory and from it use
myProject>javac -d classes src\com\DatingService\Main.java
Thanks to -d
(directory) parameter packages with all compiled classes should be placed in classes
directory (note that classes
directory must already exist). So c.class
and Main.class
will be placed in myProject\classes\com\DatingService
.
Now to run main
method from Main
class you just need to provide information about directory that contains your packages (this is ClassPath) and also use full.package.name.to.your.MainClass
. So you will have to add -classpath classes
(or shorter -cp classes
) parameter to your java
command and run it like
myProject>java -cp classes com.DatingService.Main
(note: there is no .java
suffix after Main
class since JVM is running binaries stored in .class
files, not code from .java
files)
2
solved Compile single .java file with two classes into two .class files [closed]