How to compile and run a java class with a package
Make sure that you are inside the parent directory of the package folder (the folder in which your compiled class file is), and execute the following command:
java myclass.MyClass
Below is an example file structure:
bin
-> myclass
-> MyClass.class
In the example structure above, you would have to execute the command from the "bin" directory.
Also, define the class as public
and recompile the java source file.
How to run a .class file that is part of a package from cmd?
Suppose you did cd C:/projects
and HelloWorld.class
is in C:/projects/com
, then just type:
java com.HelloWorld
How do I run a Java class in a package?
If you put the source in an appropriate directory hierarchy matching the package name (D:\javaTest\java\java\package1\App1.java
), and compile/run from the root of the hierarchy (D:\javaTest
), you wouldn't have this problem:
D:\javaTest>javac java\java\package1\App1.java
D:\javaTest>java java.java.package1.App1
App2 hello world...
You can also compile using the -d
option so that the classes are moved into such a directory hierarchy:
javac -d . App2.java
java java.java.package1.App2
Note you shouldn't use a package name starting with java
, and later versions of the JDK will throw a SecurityException. See this question for more information.
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 execute a java .class from the command line
Try:
java -cp . Echo "hello"
Assuming that you compiled with:
javac Echo.java
Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )
If that's the case and listing the contents of your dir displays:
Echo.java
Echo.class
Then any of this may work:
java -cp . Echo "hello"
or
SET CLASSPATH=%CLASSPATH;.
java Echo "hello"
And later as Fredrik points out you'll get another error message like.
Exception in thread "main" java.lang.NoSuchMethodError: main
When that happens, go and read his answer :)
Execute Java Application from Command Line without package or class specification?
When you execute java <filename>
it tries to find the file in the current directory. If it's not found, it returns an error saying could not find or load main class
.
So, in your case, you are either executing java command from different directory or you haven't compiles the class. For the former, you can specify the class file path with -cp
argument to command (e.g. java -cp FULLPATH CopyFile
), for the latter case, you need to compile the class with javac
first and then, execute it.
How do I run Java .class files?
If your class does not have a package, you only need to set the classpath to find your compiled class:
java -cp C:\Users\Matt\workspace\HelloWorld2\bin HelloWorld2
If your class has a package, then it needs to be in a directory corresponding to the package name, and the classpath must be set to the root of the directory tree that represents the package.
// Source file HelloWorld2/src/com/example/HelloWorld2.java
package com.example;
...
Compiled class file: HelloWorld2/bin/com/example/HelloWorld2.class
$ java -cp HelloWorld2/bin com.example.HelloWorld2
Using main class inside the package in java
You should use javac
, not java
only
When you use the command java
, you can execute a file, but only the classes in that file. Here you have several files, so you should compile them in order to use them.
Do the following:
$ mkdir p1
$ mv Demo.java Protection.java p1/
# edit p1/Demo.java to change `class Demo` to `public class Demo`
$ javac p1/*
$ java p1.Demo
This worked and resulted in the following:
base constructor
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
Unable to run classes with a package declaration
You are running the file from the wrong directory.
Go to /Users/eduarddedu/Desktop
and run:
javac Test/HelloDate.java
java Test.HelloDate
Related Topics
What Throws an Ioexception in Java
Difference Between Static and Final
How to Prevent Eclipse from Hanging on Startup
How to Send Java.Util.Logging to Log4J
Why Are Class Static Methods Inherited But Not Interface Static Methods
Remove Last Character of a Stringbuilder
Why Does Java Switch on Contiguous Ints Appear to Run Faster with Added Cases
How to Convert .Pfx File to Keystore with Private Key
@Generatedvalue Polymorphic Abstract Superclass Over MySQL
Java Naming Convention with Acronyms
Is -Djava.Library.Path=... Equivalent to System.Setproperty("Java.Library.Path", ...)
How to Obtain the Start Time and End Time of a Day
Does Stream.Foreach Respect the Encounter Order of Sequential Streams
Can Hibernate Work with MySQL's "On Duplicate Key Update" Syntax
Javamail with Gmail: 535-5.7.1 Username and Password Not Accepted
Convert Java.Util.Hashmap to Scala.Collection.Immutable.Map in Java