Running Java in Package from Command Line

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 do I run a Java program from the command line on Windows?

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This displays the directory contents. You should see
filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the
next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see
filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program
output.

If the system cannot find javac, check the set path command. If javac
runs but you get errors, check your Java text. If the program
compiles but you get an exception, check the spelling and
capitalization in the file name and the class name and the java
HelloWorld command. Java is case-sensitive!

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 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 to run java program in command prompt,created by intellij

Three issues:

  1. You have to specify the fully qualified class name (that means including the package name) to the java command. It looks like your myjava class is in a package com.myexample.test. So its fully qualified name is com.myexample.test.myjava.

  2. When you run the java command you have to be in the directory that is at the base of the package hierarchy (or put that directory on the classpath).

  3. You're using the src directory, which contains .java source files, but the java command expects compiled .class files, so you need to use the project's output directory. Its location in your project will depend on your IDE and configuration but it will contain same-named structure as inside src, except with .class files instead of .java files.

In your case, navigate to:

C:\myjava\sampl1\out\production\

Then run:

java com.myexample.test.myjava

How to run java files from different packages in command prompt?

Navigate to the directory that contains the compiled class files e.g. c:\myproj\classes

Then invoke java com.myproject.MyClass where com.myproject.MyClass is the fully qualified path to MyClass.

If you have dependencies on third party libraries you may also need to add the -classpath flag e.g. java -classpath c:\path\to\some\lib.jar com.myproject.MyClass

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


Related Topics



Leave a reply



Submit