I'll start of by saying Im on windows 7.
I have created a .jar file which executes fine from the command line using the - java -jar myJar.jar approach
But what I'm after is to be able to double click on the jar file and for it to open up the command prompt window and run in the command prompt as if i've just typed the java -jar myJar.jar line.
When I double click the jar file I do think it is running because a visual part of the java is appearing, but there is no command prompt window showing my console output.
After looking around I've come across people saying that javaw which is what the jar files are associated with don't have a console and that I need to associate jar files with java.exe instead of javaw.exe. I've tried this and it didn't seem to work.
Can anyone help? A step by step would be nice.
I had the same question and the bat file idea was genius and saved me a lot of time rewriting code. Thanks!(I would have upvoted,but apparently I don't have enough rep.)
Batch (or Bat) files are super easy to make.
Just put the java -jar YourFile.jar into notepad (if you're on windows), save as Title.bat, and put into the same folder as your jar.
presto! a program open-able by the general public.
This is IMHO not possible. You could open the console from the application itself, but that is OS-dependent. If you need the console open, you have to run the application from it as you already do.
If you want to display the command line you have to launch your jar with the command line.
java -jar MyJar.jar
I would do something like this:
(Tested in Widows XP with JRE 1.6, to support other OSs you should verify the path for each OS and select the appropriate console emulator (xterm, gnome-terminal... (check for existance and preference...)))
public static void main(String[] args) throws Exception {
if (args.length == 0) {
String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);//Adds extra slash (??) didn't know why
String decodedPath = URLDecoder.decode(path, "UTF-8");
System.out.println(decodedPath);
Runtime.getRuntime().exec("cmd /c start java -jar \"" + decodedPath + "\" actual_run");
}
else {
System.out.println("Hello World");
JOptionPane.showMessageDialog(null, "Hello World");
System.in.read();
}
}
Alternatively I suggest creating a bat file with this content :
java -jar yourjar.jar
This will launch your jar as well as open the command prompt automatically, all from a simple double click on a bat file.
(The bat file needs to be in the same folder as your jar file, else you need to specify the path to the jar, not just the jar name)
This is the easiest solution for beginners:
Open any text editor
write this two lines:
java "yourmainclassname"
pause
save that file as "name".bat
Run it with double click from windows GUI
(of course this new created .bat file must be in the same folder as the .class)
..but there is no command prompt window showing my console output.
No there wouldn't be a console for an executable Jar. You'll need to put that output in the GUI.
Check your MANIFEST.MF
Extract your "executable" jar file into a folder
find MANIFEST.MF in META-INF folder
check presence of this field:
Main-Class: YourMainClassHere
If this field dissapeared then open your original MANIFEST.txt for this point:
Main-Class: YourMainClassHere must end with a new line or carriage return
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
Related
I have comand line application in Java. Can I write code for double click on JAR file and app start run in comand prompt(automatic open).
Thanks for answers.
Did you try this:
Right Click > Properties > Change > C:\Program Files\Java\jre8\bin\javaw.exe
This has been resolved here How to run .jar file by double click on Windows 7 (64)
Assuming your problem is, that your jar runs silent in background and you don't see output:
Usually this is not possible using only 1 file and you would need to create a .BAT with the following next to your .JAR:
java -jar jourJar.jar
It's possible to use a wrapper, like Fast Snail suggested.
You still can do it with pure java code using 1 file with something like the following, but it's more a hack.
public static void main(String[] args){
if(args.length > 0 && args[0].equals("instance")){
//start your real application code here
}else{
Runtime.getRuntime().exec(new String[]{"cmd", "java", "-jar", "jourJar.jar", "instance"});
}
}
This will open the JAR and then it creates a new CMD process running the JAR again.
I am using a .jar file, but unfortunatley as a black box, i.e. I do not know what exactly is in there nor how it all works.
I am sending commands to the Mac terminal from a Python script. I enter the following command:
java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt
This does what I need: analyses input using the 'black box' and creates and new file with analysis output. This new file is created in the folder where jarfile.jar is located.
I want to have this file put somewhere else upon creation.
I have tried using the > operator, specifying a path, e.g.:
java -jar jarfile.jar req_data /abs_path/to/required/data input path/to_my_/input/file.txt > /output/path/
this created a file in my desired location, but it was simply the message from Terminal, saying "The operation was carried out successfully" - the analysis results file was created in the same folder as before.
I tried %*> too, but it threw an error.
As a poor workaround I now have a function, which retrospectively finds and moves all the newly created files (analysis output) to my desired folder.
Is there a way to control the output files with the command line within the original command? Or is it something that is specified somewhere in my jar file? My problem is that editing it is not allowed.
I'm new to python. However, I may suggest to try few things, if they can work for you. Apology me, if does not work! I believe that you have already done the following step:
import subprocess
subprocess.call(['java', '-jar', 'Blender.jar'])
Like, if you have a properly configured jar path, then can run jar directly.
Secondly, look at the cwd parameter (is used for executable). Include a cwd param as x
def run_command(command, **x):
with subprocess.Popen(command,...., **x) as p:
for run_command specify the path of either the working directory (possibly it should be) or the full system path. I'm not sure, just try both.
for outputline in run_command(r'java -jar jarfilepath', cwd=r'workingdirpath', universal_newlines=True):
print(outputline, end='')
Alternatively, you can try to run command from the directory in which you wish to store output file. Try: run the popen as
subprocess.Popen(r'directory of running command', cwd=r'workingdir')
where workingdir could be your current directory path.
If it does not work, try without r'. If still does not work, try doubling slash in the path like (C:\\ abc\\def)
I'm very new to programming so let me just explains where I'm at:
I have downloaded the most recent JDK
I have changed the path variable in the environmental variables tab, javac does run by itself in the command prompt
I have downloaded notepad++
So I created a very simple program, pretty much just a simple "hello world" deal...
public class pleaseWork {
public static void main (String[] args) {
System.out.println("Please work");
}
}
And saved it into a folder I have on my C drive but not anywhere in the java folder. I have it saved as pleaseWork.java.
So I go to the command line and if I just type javac it runs correctly, but if I type in javac pleaseWork.java I get an error -
javac: file not found: pleaseWork.java
So basically I'm asking if I need to save my notepad++ .java files in a certain place for them to compile in the command prompt or is it something else?
Error javac: file not found: pleaseWork.java indicate that your javac command not able to locate file you have given in your command that is pleaseWork.java.
To compile file place in any folder you need to go upto the path where your JAVA file place, from that path execute command javac pleaseWork.java.
You are executing your javac command from the directory path where this java file is not present.
Another way is
You need to specify the full file path in the argument.
e.g javac "C:\temp\pleaseWork.java"
For more info visit How to Run JAVA file from command promt.
when you go to command prompt... first of all go back to c drive and set the path there. and compile it... and while saving program in notepad... please provide file name in double quotes like "programWorld.java" or else it will be saved as text file not java file. resulting in file not found error
Make sure javac is in your path (so you can run it from anywhere). Then in your command prompt cd to the directory where your pleaseWork.java file is saved and call javac from there.
As an aside, it's standard form to name your classes and files in java starting with a capital letter (PleaseWork.java)
To set the temporary path of JDK, you need to follow following steps: Try this and let me know if its still not working, then c:>cd yourNewFolderName then set path
Open command prompt
copy the path of jdk/bin directory
write in command prompt: set path=copied_path
For Example:
set path=C:\Program Files\Java\jdk1.6.0_23\bin
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, StringIndexOutOfBoundsException
{
Runtime.getRuntime().exec("cmd /c start C:\\dig-files3\\query3.bat");
}
}
I'm trying to launch a batch file through a java program but I get a 'dig not recognized as an internal or external command ...' message in the cmd screen. However when I double click on the batch file in the window it runs fine. How can I fix this?
Here is the batch file's content:
SET /a VAR=0
:HOME
SET /a VAR=VAR+1
IF %VAR%==200000 goto :End
dig #10.3.1.166 6.4.0.3.5.5.5.9.9.9.com. naptr
goto :HOME
:END
This is probably happening because "dig" has not been added to your PATH variable. Try opening a new terminal window and typing "dig" and it will probably show the same error. You have to go to Control Panel -> System -> System Properties -> Advanced options tab -> Environment variables.
There you have to search for the PATH variable and add, at the end (and after adding ";" to the last command) the full path to "dig" executable (except for the executable itself e.g. c:\foo\bar). Then try again. This environment variable tells Windows to look on the list of paths contained in it, for the executable you are trying to run.
Another solution is to copy over your compiled java file to where the dig executable is located and run it from there.
You should create a file object for the working directory to prevent problems with whitespaces in the path and then use that object to start the batch script:
File workdir = new File("C:\\dig-files3");
Runtime.getRuntime().exec("query3.bat", null, workdir);
There's also a flaw in your batch script: You probably want to write SET /a VAR=%VAR%+1 so that %VAR% gets evaluated before incrementing it.
Your problem is that you do not have the batch file in the system PATH variable. Insert the path to your batch file into the system PATH and it should work fine
Ok, there may another way to fix this but this is how I did it. I am using Eclipse and I copied the dig application to the project directory C:\User\username\workspace\projectName
i have tried several things so far, i have used a .bat file to run it, and it opens up the command prompt, but closes right after opening. i have a manifest.txt file already created along with the .jar. If i type -java -jar DesktopApplicationRunner.jar in a command windows that is already up it will run the program.
my .bat currently says:
-java -jar DesktopApplicationRunner.jar
Apologies just read your comment saying you have inputs. your problem is using the bat file, unless you have user inputs, the application will start, open the command line, then close when finished, the best way is to manually type in the command line, so it doesn't close the view down afterwards, sorry if this makes no sense.
-Ian