A
A
AlexandrMa2022-02-15 16:22:23
Java
AlexandrMa, 2022-02-15 16:22:23

How to compile application into one file?

I am creating a project. Through Project Structure I connect the necessary libraries. I write the code, and I press Build Project. All without errors. Works.
As a result, the Main.class file is located in the
C:\Users\alexey\IdeaProjects\app2\out\production\app2\com\company
folder. I cut it into C:\tests

But I can't run it from the command line

C:\tests>java Main.class
Error: Could not find or load main class Main.class
Caused by: java.lang.ClassNotFoundException: Main.class


1. Do I understand correctly that at the time of creation, I should get one file, complete and independent, which I can run on any other computer?
2. Why doesn't my Main.class run?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2022-02-15
@AlexandrMa

Do I understand correctly that at the time of creation, I should get one file at the output

No, there will be as many files as there are classes.
complete and independent

And besides application classes, dependency classes will also be needed.
At the top of my project it says
package com.company;
I want to run from C://Main.class drive

Why isn't my Main.class running?

First, java does not accept a filename, but a classname. Secondly, the directory structure in the classpath must follow the structure of the packages. That is, Main.class should be C:\com\company, and the launch command should look like this . java -cp . com.company.Main
To get one file that can be run from different places, you need to build jar .

N
Nikolay Savelyev, 2022-02-15
@AgentSmith

https://stackoverflow.com/questions/1279542/how-to...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question