making a java package in the command line
packages are just directories on the filesystem.
so your package: com.mycompany.util
corresponds to a directory com/mycompany/util
.
When running and compiling etc your current workding directory should be where that top directory is located.
To include libraries, include them in your classpath when compiling and running. For example make a Project directory myproject
and under that have your java-files and packages under myproject/src/
and libraries that you use under myproject/libs/
Then when your current workding directory is myproject
execute java -cp .:libs/*.jar
or the same with javac
.
But I suggest you look into using ant or maven.
How do I compile my main Java program on the command line when it includes an external java package?
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name import org.somepackage.Human;
import all package content import org.somepackage.*;
or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram
Running java in package from command line
You'd run it as:
java One.Test
... but from the root directory (basic
), not from the One
directory. You always specify the fully-qualified class name.
Oh, and package names in Java should be lower-case, so it should be one
and one.inner
, not One
and One.Inner
. Just a convention, but one which pretty much everyone follows.
How to compile java package structures using javac
Are you sure importpackage/subpackage is in your classpath?
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
If the -sourcepath option is not specified, the user class path is also searched for source files.
If the -processorpath option is not specified, the class path is also searched for annotation processors.
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
How to build a java project using the command line?
If you are a student willing to have a Java programming career, it might help to learn how to do things from command line, e.g. edit the files, compiling the classes, testing and building the project. Oracle tutorials provide example on this matter: https://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html#win32-2
However, I strongly advise you to embrace an IDE as your Java career will mostly reside in an IDE as real life projects are BIG! There are tons of helpful things the IDE does to you out of the box or to simplify things. Since you are a student, I will give you one basic example besides compiling: a class with 10 fields requires you some typing for getters, setters, hashCode, equals. Alternative? Few keystrokes to instruct the IDE to generate them for you. And that's one basic example.
Regarding the project structure, embracing the (since you mentioned it) Maven project structure of src/main/{java,resources}, src/test/{java,resources}
even if you do NOT use Maven. This will let you forget about organizing the files around.
If you were asking about structuring the classes in the right packages, you will figure out yourself as you gain experience. Rule of thumb is to group classes together by functionality they provide. Additionally, if the packages are organized right, if you change something and touching a few classes, ideally you'd want the changed classes to be located in a single package if possible.
Learning Maven is a good choice as it is a powerful tool for building a project and keeping things organized (project structure, project dependencies, etc.).
How to import JAVA packages in cmd?
You shouldn't set the classpath using an environment variable as it is bad practice. What if you accidentally change it later for a different project and your current project breaks?
When including classes in the classpath, you can include the path of the root of the package of the class, as in the folder that contains the folders in the package structure. However, when you're including a jar in your classpath, you need to put the entire path of the jar file (relative to the current working directory) all the way up to the jarname.jar.
Also, remember that by default, java looks in the current working directory and uses that as its default classpath. However, as soon as you specify a classpath it no longer does that automatically for you. Be sure that you're including your current directory in your classpath as well.
Finally, be sure to surround the classpath in quotes otherwise java might think its a part of another argument.
I would try this:
javac -cp "./;log4j.jar" TNT.java
And then to execute the class file:
java -cp "./;log4j.jar" TNT
Hope this works, good luck!
Related Topics
Cannot Run Program "Mvn" Error=2, No Such File or Directory
How to Run a Spring Boot Executable Jar in a Production Environment
How to Get Desktop Class Supported Under Linux
How to Set Java_Home Path on Ubuntu
Problems with Java3D Lib Configuration
How to Run Java Application on Startup of Ubuntu Linux
Java Animation Programs Running Jerky in Linux
How to Find My Pid in Java or Jruby on Linux
Linux Launch Java Program on Startup (Ec2 Instance)
Java System Preferences Under Different Users in Linux
Hibernate: Hbm2Ddl.Auto=Update in Production
Why Do I Get "Exception; Must Be Caught or Declared to Be Thrown" When I Try to Compile My Java Code
When Exactly Is It Leak Safe to Use (Anonymous) Inner Classes