Skip to content

Instantly share code, notes, and snippets.

@bbrother92
Created December 15, 2017 09:08
Show Gist options
  • Save bbrother92/bdf0ecf27019c91df0b3eefe0c64151a to your computer and use it in GitHub Desktop.
Save bbrother92/bdf0ecf27019c91df0b3eefe0c64151a to your computer and use it in GitHub Desktop.

Compilation

In root directory: javac -d target/classes src/main/java/app/Main.java src/main/java/util/Second.java (-d option to set output dir)
Or javac -cp src/main/java/ src/main/java/app/Main.java

java command

Assume there is a source code file (src/main/java/app/Main.java):

package app;

import util.Second;

public class Main {
	public static void main(String[] args) {
		System.out.println("inside test main");
		new Second().mtd();
	}
}

And second file is (src/main/java/util/Second.java):

package util;

public class Second {
	public void mtd () {
		System.out.println("inside Second");
	}
}

And Main.class placed in /target/classes/app (Maven structure).
So in order to run you have to use this command: java -cp target/classes/ app.Main

JARs

in target folder after packaging project into jar: java -cp my-test-1.0-SNAPSHOT.jar app.Main runs main class

Another way: java -jar myCopy.jar but here you need to specify MANIFEST.MF:

Manifest-version: 1.0  
Main-Class: Test

and create jar: jar cfm MyJar.jar ../MANIFEST.MF app/*.class util/*.class

More on jars: docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment