Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running java in package from command line

I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again.

I have two simple classes,

package One; import One.Inner.MyFrame; public class test {     public static void main(String args[])     {         MyFrame f= new MyFrame();     } } 

And the other class is,

package One.Inner; import java.awt.*; import javax.swing.*;  public class MyFrame extends JFrame {     public MyFrame()     {         setPreferredSize(new Dimension(400,560));         setVisible(true);     } } 

I am at base folder "basic" in Windows cmd. I compile using

basic> javac *.java -d . 

A folder and subfolder is created.

cd One basic\One> java test 

This generates a big set of errors. Many answers directed to specify the full path which didn't work. My classes are in One so specifying One using -cp didn't work either.

like image 262
user2756339 Avatar asked Oct 17 '13 17:10

user2756339


People also ask

How do I run a Java program in a package?

How to run java package program. You need to use fully qualified name e.g. mypack. Simple etc to run the class. The -d is a switch that tells the compiler where to put the class file i.e. it represents destination.

How do I run a package from the command line?

Open a command prompt and navigate to the compile-packages-in-java directory. Then type in the command to compile the Person source and hit Enter . Notice that you provided the path of the Java file. This path corresponds to the package name.


1 Answers

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.

like image 54
Jon Skeet Avatar answered Oct 10 '22 01:10

Jon Skeet