I have the following package on Eclipse:
com.mortgageapp.projects.app
I'm not interested about the package format at the moment, it's just testing. But I'm wondering how to run the app from the terminal (Windows and Mac)?
It contains a Main.java file where it will begin so I have tried locating and entering the src folder. Then doing something like: javac com/mortgageapp/projects/app/Main.java (or: javac com/mortgageapp/projects/app/*.java).
Just wondering if this is current as when I then do: java com/mortgageapp/projects/app/Main I get a few errors.
Your compilation is probably okay, but to run it you need to specify the class name, not a filename:
java com.mortgageapp.projects.app.Main
That's assuming the current directory is in the classpath. If it's not, you may need:
java -cp . com.mortgageapp.projects.app.Main
I assume you are writing an RCP application.
Export the project as a RCP application (click the "export" link in your product configuration). You should get a runnable application file.
If you really want to run the application from the terminal, you need to use the main function in the EclipseStarter. But you don't want to go there unless you are doing something very special.
Related
I was able to run the JAR file inside IntelliJ when I do Shift + f10.
However when trying to execute the JAR file from my directory, nothing happens. My META-INF is place as followed here: https://stackoverflow.com/a/49147689/12973878.
This is my JAR structure
JAR structure
File structure Image
Image
Can I know what is the problem here?
Based on the description, I assume you're in a windows environment. Try to run it in terminal. Change the current directory to the location of the file and the terminal command you need to give is:
java -jar <jarfilename>
Some suggestions:
Use the -verbose:class option when starting the java application to determine if classes are loaded from the correct places.
See if there's additional settings in the run configuration of IntelliJ. You'll find it in the upper right corner of the editor window next to the executuin button - dropdown will show 'Edit Configurations'.
If there's really no error at all - are you sure you have the correct starting Class as entry point? You might simply be executing the wrong main method from terminal while
IntelliJ picks the correct one (look for "main class" in run configuration).
After running a build for the project (or a mvn package) your jar is placed in <your_project_path>/target.
There you will find: <your_project_name>-0.0.1-SNAPSHOT.jar. Now just open command prompt (on win) / terminal (on mac).
Go to the project path
cd <your_project_path>/target
and run
java -jar <your_project_name>-0.0.1-SNAPSHOT.jar
Don't forget to check that you have java path in your system $PATH
run to verify
java -version
I'm new to Java11/all the overcomplicated module stuff.
The Problem
So I exported my Java11/JavaFX11 program from Eclipse as a Runnable JAR. If I click the JAR, it runs perfectly fine (Eclipse includes all of the module settings and JavaFX itself automatically in the runnable JAR). However, if I try to bundle the JAR with a JRE and run it via the command line with the following BAT file:
#ECHO OFF
%~dp0\jre\bin\java -jar javaprogram.jar
pause
I get:
Error: JavaFX runtime components are missing, and are required to run this application
Press any key to continue . . .
How can I get it to just run the JAR file like it does when I click it?
Ways I've tried to fix it
The weirdest part is, if I just use:
java -jar javaprogram.jar
Which just accesses the installed JRE, it works again. It's only when I'm directly pointing it to a JRE at a specific path that it appears to break.
Alternatively, I'd just bundle JavaFX beside the JRE, but there doesn't seem to be a way to call --module-path with a relative path (googling this nets me a bunch of entirely unrelated stuff). It seems to demand an exact path, which isn't going to work if people are downloading a zip archive and extracting it. This would be redundant though because Eclipse is already packaging JavaFX with the JAR. I don't know why it's getting confused just because I'm calling it from the command line.
The project's code
The project I'm trying to get this to work with happens to be open source, so you can check out the code for it here:
https://github.com/SkyAphid/JDialogue
The main class is JDialogueCore.
Closing
I don't want to use installers since I think that's too bloaty. I'd like to be able to deploy my software like I always have by just putting them in an archive you can extract and run.
It's difficult to simply Google the problems as well since I keep getting completely unrelated results due to the broadness of the topic. Any direction/documentation relating to this problem would be greatly appreciated.
Thank you for your time!
While Java 8, 9, and 10 allowed a JavaFX Application subclass to act as a main class for program startup, that is no longer the case as of Java 11. Placing your public static void main method in a different class and making that class the main class solves the problem. (Source: https://github.com/javafxports/openjdk-jfx/issues/236)
Your command line invocation needs to specify both the location of the JavaFX jar files, and the location of JavaFX native libraries. Normally these are the same location in the JavaFX SDK, but they must be specified in different ways: the jar files go in the classpath or module path, while the native libraries’ location must be specified in a system property:
cd /d %~dp0
jre\bin\java -cp javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib com.example.MyNonApplicationClass
If you define a module-info.java in your program, your .jar is a modular .jar, and you can benefit from the additional security of modules:
cd /d %~dp0
jre\bin\java --module-path javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib -m com.example.myapp/com.example.MyNonApplicationClass
If your modular .jar file has a main class defined, you can omit the class name:
cd /d %~dp0
jre\bin\java --module-path javaprogram.jar;javafx-sdk-11\lib -Djava.library.path=javafx-sdk-11\lib -m com.example.myapp
Notice that relative paths work just fine with --module-path. Relative paths use the current directory as a base. The current directory is not changed merely by putting %~dp0 in front of the invocation of Java. The current directory is a property of the command line or script actively running, and can only be changed with commands like cd or pushd.
This is my first question on stack overflow.
Please have a look at this first, please!!
I am creating an executable jar file by clicking on the button surrounded with grey color. The problem is that the exxcution of the application always starts from the "PersonalAssistant.java".
I want my application to begin its execution from "App.java" which also contains a main() method.
So, basically the jar file which I want should begin its execution from App.java class which has a main method.
I don't know how to change the path or execution class for jar.
Please help me!!!! I'm stuck with this silly problem. But I want a solution for this.
I'm using Windows10 with Netbeans 8.2 IDE
You should be able to reconfigure your IDE and rebuild the executable jar specifying which class you want to use as the executable.
in your IDE add java -cp myjar.jar App
to your compile command
this might also help
Look at the picture above. Right-click on your project and select Properties. Select Run. Look for the Application Class field. In my example, I have javafxapplication211.Main. This structure is projectName.MainClass. You should just need to change the MainClass part to the class you want to start your project. For example, if Main.java was starting my app and I wanted to change it to App.java. I would change javafxapplication211.Main to javafxapplication211.App.
If you hit Browse, it will give you a list of available classes that can start the application.
I'm currently in the process of learning more about Java. I downloaded a sample program that has about 5 different .java files in the source folder.
Whenever I try to execute the class files in the command prompt using "java ClassNameHere", I get a message in the cmd prompt that says "Error: Could not find or load main class ClassNameHere".
I've tried recompiling them using the javac command and they compiles without any issues. Its just the class file i'm having trouble with.
In addition to this, I also cannot run the java files using Eclipse. Instead of getting an option to "Run as Java Application", "non applicable" appears under the "Run as" tab in Eclipse.
I've tried a number of different solutions already posted online, including another command that uses "java -cp . ClassNameHere" or something along those lines and they have not worked for me. Any help is appreciated.
Edit
Link to program from my drive since I'm not home
https://docs.google.com/file/d/0B9_pcTVZTnfEeVdzZmM0ZmFkTmc/edit?usp=docslist_api
Here is a a procedure that will get the application working in Eclipse. This procedure assumes the following:
Windows 8. I don't think Linux/Mac OS X procedure would differ greatly, but just in case...
In Eclipse:
Right click in Package Explorer, and select New --> Java Project.
In the New Java Project dialog, uncheck Use default location, and click Browse and select the directory you've extracted the zip to, or type the path in the box.
Click Finish.
From this point, you can run this is a Java Application from Eclipse.
Confirm that at least one of the classes has a main() method. That would be required for Eclipse to show "Run as Java Application".
Follow this steps:
Run Command Prompt.
Use cd C:\path\to\your\porject\java classes.
Use set path=%path%;C:\Program Files\Java\jdk1.7.0_65\bin (CHANGE THE PATH TO YOUR jdk1.x.x_xx\bin FOLDER) to tell the system where to find jdk programs.
Use javac MainClass.java to compile the project, MainClass.java si the java class where the main method is.
Use java MainClass (WITHOUT .java EXTENSION) to run the project.
After the last command your project should run correctly.
Sorry for the "use" ripetition but i'm not english.
I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.