简体   繁体   中英

How to execute a large compiled Java project via command line, without using any software (Windows)?

I have a Java project with 5 packages and 30 classes. I want to test this project on a different computer, but I can't install any sotware on that computer so I can't use things like Maven, Eclipse etc. Is there a way I can execute the program on that computer?

What I tried to do, is to compile the project using Eclipse on my computer, then went to the other computer and tried to execute the project main class via the folder that the main class .class file is at.
IE, say that the main class name is Hello in package Greetings and Hello.class is at folder named folder . So I opened the command line window at folder and typed the command:

java Greetings.Hello

That didn't work....
Edit: After doing this I got the message: Error: Could not find or load main class Greetings.Hello

If the package name is Greetings and you want to run Hello.class

  1. Hello class must have main method.

  2. Hello.class must in folder name Greetings (package name).

  3. Execute java Greetings.Hello from the one level above of Greetings folder

    It seems to me Hello.class is not inside of Greetings folder

If javac is installed on the system you can directly compile on the system. you can compile even large projects including many packages with choosing different options provided by javac.

The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files. It can also process annotations in Java source files and classes.

There are two ways to pass source code file names to javac:

For a small number of source files, simply list the file names on the command line. For a large number of source files, list the file names in a file, separated by blanks or line breaks. Then use the list file name on the javac command line, preceded by an @ character. Source code file names must have .java suffixes, class file names must have .class suffixes, and both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.

Inner class definitions produce additional class files. These class files have names combining the inner and outer class names, such as MyClass$MyInnerClass.class.

You should arrange source files in a directory tree that reflects their package tree. For example, if you keep all your source files in C:\\workspace, the source code for com.mysoft.mypack.MyClass should be in C:\\workspace\\com\\mysoft\\mypack\\MyClass.java.

By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d source

Given that you have Eclipse and you ran the code in Eclipse: the quickest way is to use Eclipse and export it to executable JAR.

If you have Run Configuration (eg named Hello) that you use for running the code:

  1. Menu -> Export -> Runnable JAR file
  2. Launch Configuration: Hello
  3. Select export destination: (eg C:\\tmp\\Hello.jar)
  4. Set Extract required libraries into generated JAR
  5. Click finish.

This will create Hello.jar file you can execute typing in: java -jar Hello.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM