I made a Java program that you can run :
1 - Only with the GUI by launching the .jar .
2 - With the GUI and a console by launching a .bat .
java -jar "app.jar"
PAUSE
But now I would like to add the possibility to launch it ONLY with the console, that means without the GUI, I searched but didn't found how to do that.
I understand your question like: the application could work console-only, but it starts the ui always.
In that sense, look into you main method /class and simply check for the command line args. Maybe you simply add some -nogui parameter which you then use to put a condition around the launching of any gui components.
And given your comment: you have to compare your args like:
.... void main(String args[]) {
for (String arg : args) {
if (arg.equals(...
See here for more details regarding command line options.
Related
I wrote a java file including javaFX. Now, I want to run this file, like
java -jar example.jar
But I'd like to suppress the graphical output.
Is there any possible, like a flag or anything else, to do this?
My program normally shows a progressbar and after that a video of the simulation.
Thanks a lot.
To elaborate on JB Nizet's comment.
JAR files have manifests. In order to run your JAR using the command java -jar example.jar, the manifest must have a Main-Class entry. And your main class must have a main() method.
So launch your app like so...
java -jar example.jar NO_GUI
And in your main() method, write something like the following...
public static void main(String[] args) {
if (args.length > 0 && "NO_GUI".equals(args[0]) {
// Don't show GUI
}
else {
// Show the GUI.
}
}
You could call hide() on the scene so the window dissapears.
There is not really a way to force this.
Instead, implement it as feature. Create a command line argument nogui and react to it:
public static void main(String[] args) {
boolean useGui = true;
if (args.length > 0 && args[0].equals("nogui")) {
doNotUseGui = false;
}
// Create your program and give it the flag
Program prog = new Program(useGui);
...
}
Note that theoretically it would be possible to hack your application and remove any such calls, or to suppress the calls on a native level. But I guess that is not really the route you want to go.
Relevant Links:
Java: Passing combination of named and unnamed parameters to executable Jar/Main Method
Passing arguments to JAR which is required by Java Interpreter
I understand how to pass strings from the command line to execute my main method:
java -jar myApp.jar "argument1"
My question is: is it possible to set up my main method in a way that would accept:
java -jar myApp.jar -parameter1 "argument1"
Here is my simple main method for context if you need it
public class myApp {
public static void main (String[] args){
System.out.println("Argument1: "+args[0]);
}
}
Thing is: whatever you pass on the command line goes into that args array. To be precise:
java xxx -jar JAR yyy
xxx: would be arguments to the JVM itself, like -Dprop:value for properties
yyy: are passed as arguments to your main method
So, when you pass "-parameter 'argument1'" then ... that is what you will see inside main!
In other words: the idea that some command line strings are "arguments"; and other are "-switches", or "--flags", or "-h" shortcuts ... you simply have to write the code to do all of that.
Luckily, there are plenty of libraries out there that help with that; see enter link description here
In an effort to make my app more OSX friendly, I am trying to set the dock name of my program to something like MyApp instead of a fully qualified class name (the default), such as myproject.mypackage.mysubpackage.myclass. Obviously, the first is much more appealing.
To do this, I use the OSX -Xdock:name command as a command line option when executing my .jar file. So to execute it, the command might look something like java -Xdock:name=MyApp -jar /mypath/myjar.jar. This works perfectly and sets the .jar's dock name to MyApp. But the issue is that this .jar will never be executed via command line and will be a double-clicked runnable .jar with a GUI display.
The only way I have thought of to set this command line option programmatically is to have a second class execute the class that actually starts the program. So something like this:
public class AppStarter {
public static void main(String[] args) {
String cmd = "java -Xdock:name=MyApp -cp myproject/mypackage/AppBuilder";
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec(cmd);
} catch(IOException ex) {
//Display error message
}
}
}
public class AppBuilder {
public static void main(String[] args) {
//Start actual program and build GUI display
}
}
So here, AppStarter sets the command line options for AppBuilder, which when executed, has the dock name MyApp. The problem I see with this is that it is very tightly coupled. If for some reason the command line is inaccessible on the device or some IOException keeps getting thrown, literally nothing will happen with the program and it will be dead. There would be no way for the average computer user to recover from this.
So I'm wondering if it is possible to perhaps set these command line options after the .jar has already started executing. The old way to programmatically set the app's name has been ineffective for several OSX updates, so I'm stuck with only this command line option. Thanks for any advice.
Once the java command is executed, the command line arguments are parsed and set for the running JVM. You cannot change it any more.
This is usually handled by execution scripts (bash, etc.). If you cannot use them, you can use your approach, but the biggest disadvantage is that it will be running in a separate process.
I have the following line in a batch file.
java Client "127.0.0.1" 9876
It contains the name of my java class and two arguments. My application requires these arguments to run properly.
Is there any way to pass these arguments when running the application in eclipse? It would make debugging a lot easier. Of course I could resolve the problem by using the values of the arguments in the code but I'm curious.
Instead of just hitting the "Run" icon, select the dropdown box next to it, and choose "Run Configurations". Find your application (or create a Run Configuration for it) and put the command line arguments in the "Arguments" tab. See the docs for more information. It should look like this:
See the run configurations. You can specify arguments. You can even prompt the user for arguments, along with defaults:
${string_prompt:host:127.0.0.1} ${string_prompt:port:9876}
The first prompt is host, with default value 127.0.0.1 filled in. Second pop-up has the prmpt port, with 9876 filled in
Right-click on your project.
Go to Debug As > Debug Configurations or Run As > Run Configurations.
Click the tab that says Arguments.
Enter in your Program Arguments
Click Apply or Debug
Want to add something like, how to add multiple parameters.
Right-click on your project.
Debug > Debug Configurations
Go to Arguments tab.
Enter in your Program Arguments, each separated by a new line. (e.g 3 arguments in attached image)
Click Apply or Debug
Hope it helps.
From "Run" go to debug/run configurations. Click the tab called "Arguments". You can give the program arguments there.
Run configurations > Arguments tab. Just put "127.0.0.1" 9876 in the program arguments.
Run-> Run Configurations->Arguments->Enter your arguments separated by space->Apply->Run
Ensure that the right project name and it's main method are selected under "the Main" tab under run configurations
this work for me, in public static void main method.
public static void main(String argv[]) throws Exception {
int port_com = 2;
boolean debugMode = true;
int socket = 6789;
HasarMain hasarMain = new HasarMain();
// Check if a command line argument exists
if(argv.length != 3){
System.out.println("Missing, Port - socket - debugMode!");
System.exit(0);
}
port_com = Integer.parseInt(argv[0]);
socket = Integer.parseInt(argv[1]);
debugMode = Boolean.parseBoolean(argv[2]);
Run-> Run Configurations->Arguments->Enter your arguments separated by tab->
${string_prompt:argv:"2" "6789" "true"}
I have an error in my first step with Java, so when i try to run the code hello world:
class apples{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
I go to: - Run as .. -> Then i choose Java aplicacion - > And i press Ok
But when i press Ok does not appear the window down to show me the correct message Hello World
Your code works fine for me:
class apples
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
I downloaded it to c:\temp\apples.java.
Here's how I compiled and ran it:
C:\temp>javac -cp . apples.java
C:\temp>dir apples
Volume in drive C is HP_PAVILION
Volume Serial Number is 0200-EE0C
Directory of C:\temp
C:\temp>dir ap*
Volume in drive C is HP_PAVILION
Volume Serial Number is 0200-EE0C
Directory of C:\temp
08/15/2010 09:15 PM 418 apples.class
08/15/2010 09:15 PM 123 apples.java
2 File(s) 541 bytes
0 Dir(s) 107,868,696,576 bytes free
C:\temp>java -cp . apples
Hello World!
C:\temp>
Your lack of understanding and the IDE appear to be impeding your progress. Do simple things without the IDE for a while until you get the hang of it. A command shell and a text editor will be sufficient.
Sorry about missing javac; cut & paste error.
If you look at the screenshot, your class name is there, last in the list. Select it and press OK. To not see this message again, right-click on the class name on the left side and select there Run...->Java Application.
The only problem that causes your error here is that the classname and the filename do not match - and they have to.
Solution
Rename either the file thesame.java to apple.java or the class to thesame. Then if you select "Run as..." again, eclipse will present a menu item to start your Java application.
(other mentioned, that there's no requirement that a top-level class and the filename do match - unless the top level class is public. Of course this is true. But the problem was about "running" a class under eclipse as a Java application)
Try public class apples and make sure the file is apples.java. Also it should be public static void main(String[] args)
You have 2 classes by name of "thesame.java" under the source folder. Since one is directly under the src folder, and other under (default package), they use the same namespace, hence Interpreter is confused which java file to execute and is asking you to select the class you want to execute.
Class names must be capitalized... so change apples to Apples. Also, if you are a beginner (which it seems like), I would recommend the Netbeans IDE -- it's a bit more friendlier for new users than Eclipse.
You class must be named "thesame" if you store it in a file called "thesame.java", as you have. Either rename your class to "thesame" or change the file to be "apples.java".
You should move the "[]" to be before "args". So, String[] args.
Either select "apples" at the bottom of the menu you posted and run it, or right-click on the Java file and make it the default thing to run for this project. Or launch it by right-clicking on the file and selecting "run".