Search code examples
javaclasscompilationjavac

How to compile several java files which depend on each other?


I have few java files. Main.java uses Picture class from Picture.java file. I want to know how to compile and run Main from command line ?

Here is Main.java:

package com.company;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    static Picture pic = null; // Picture class ???

    public static void main(String[] args) {
        long t1, t2;
        String name = "bears.jpg";

        pic = new Picture(name);
        t1 = System.nanoTime();

        pic.new_img = meanFilter(pic.img);

        t2 = System.nanoTime();
        pic.writeImage();
        calculateTime(t1, t2);
    }

and Picture.java:

...
public class Picture {

    public BufferedImage img;
    public BufferedImage new_img;
    ...

Solution

  • Assuming both classes are in the same directory use :

    javac Picture.java Main.java
    

    This way the dependent class (Picture.java) is compiled first before your Main.java

    To run it you will need to specify the entire package structure and run it from the src directory :

    java com.company.Main