I have a very simple java application that takes in strings and arranges them into lists based on the user's specifications. It has two files. Right now there's no GUI: I run it through the command line. Is it possible to compile it into a program that can be downloaded and run by clicking on an icon even without a GUI (a terminal window will pop up when you click on the icon) and have it still function? If so, how do I do so?
If you have Linux & a GNOME desktop, run 'gnome-desktop-item-edit' from terminal, set the command to whatever, and change 'application' to 'application in terminal'. In windows, create a .bat file that looks something like:
#echo off
java [-jar] <path to file>
And a terminal window will pop up that will exit when the command is done.
I don't know about others, but I'm sure there is a way.
Yes you can.
However, there will be some surprising behaviour.
First off, you can double click jar-files to run them. So what you want to do is to build a jar file.
I find that mkyong has a nice tutorial here
Now of course, that's how to do it manually. What you probably want in the long term is a way to do that automatically.
For long term (I am assuming you're a first year student or similar) use, I'd recommend learning Ant or Maven. These are tools that will compile your program, test them and if you tell them to, make jarfiles for you.
While I personally find Ant easier to get started with, but Maven is very widespread in the industry.
Now, let's say that you create the jar, you double click on it, and you enter your strings etc. and all is well... And then it all disappears.
This is because Windows will close terminal windows when they're done. So you'll have to remember to ask for an extra Enter keystroke or something before exiting.
This can be annoying but it's not insurmountable.
Related
I'm building a desktop app that uses Cygwin to execute shell scripts on Windows 10. Ideally, users wouldn't have to install Cygwin because I'm putting the relevant exe files in my project. I have Cygwin's "sh.exe" in my project which I can call without an absolute path and it seems to be running grep, zcat, awk, etc with no issues.
It looks like I'm running into the issue explained in https://www.question-defense.com/2010/08/25/windows-7-cygwin-sort-input-file-specified-two-times where Window's cmd is using it's own sort rather than Cygwin's sort.exe and you have to put the path of the sort.exe in the script. So it looks like the user would have to download Cygwin which would somewhat defeats the purpose of my project because I want a hassle-free experience for users. I don't want them to have to download dependencies.
The only resolution I've thought of is to include the sort.exe in the project and replace the "sort" string in the script with the Windows path of the sort.exe, which I think would be in the directory where the user launched the app; maybe System.getProperty("user.dir").
Is there a better solution?
MobaXTerm seems to have got it down. They have a CygUtils plugin, which you have to download and put in the right place, but I imagine it wouldn't be hard for them to have their application come with CygUtils. I'm trying to do something like that.
I faced same issue while using Cygwin.
I renamed [cygwin64_folder]\bin\sort file to csort (or any other convenient name if you wish) and used csort command for my usage.
I made a version of an 2D game by using an applet, java awt and some little java swing. I made a start-class containing a main method in order to export it as a runnable jar file.
The exporting works fine, without any problems.
But when its done and i try to double click the runnable jar file, nothing happens.
When I test it using the run button in eclipse everything works fine...
In the first moment I thought that my start-class may be the issue, but that's not the case. I tested it on an other applet, and there the runnable jar file using the same start-class works fine.
Now my question is: What could be the issue that prevents my runnable jar file from doing stuff when I double click it?
I found the mistake.
I've imported all the pictures i used in this game by using ImageIO.
I typed the names of the Images without a capital letter at the beginning, whilst the pictures in the folder actually had a capital letter.
Java eclipse did not care about it and it worked when i tried it out, but when i tried to open it with the command line, it didnt work aswell as it didnt work with a normal doubleclick.
i hope this helps others who got the same issue ^^
try right clicking on the file, select 'open with', 'Choose default program' and then select java Java(TM) Platform SE binary. If you don't see that option you should search for the javaw.exe in your java installation folder and select it. Also you should check Use the selected program to open this kind of file
So,
I have made a little game in eclipse, it generates a rondom number from 1 - 1000 , when playing out of Eclipse, it plays in the eclipse console, but what if i want to play it without eclipse?
There is no console showing up?
Anyone a idea on how to fix this?
EDIT : I would like a new GUI instead of CMD.
Grts
PS: Tell me if you need a specific part of the source code
1) You should probably export your program as a .jar file.
This isn't absolutely necessary - but it's better. And it sounds like you've already done it :)
2) Be sure to specify the main class in Eclipse before you export the .jar
3) Once you have the .jar, you can:
a) execute "java -jar myjar.jar" from a command prompt (for a console-mode program)
b) execute "javaw2 -jar myjar.jar" from a command prompt (for a Swing/GUI program)
4) You can also set a "file association" with javaw in Windows so that you can just double-click on the .jar to execute it:
Running JAR file on Windows
It seems the OP wants to reroute console output to a location of his choosing, not open an actual shell.
Here is one way to reroute stdout.
You can send it to a string, stream, file, etc. and then print it yourself into a window.
If you want a GUI, you will have to develop one yourself. There is no "Java Console GUI" that I've ever heard of.
I encountered this problem several times. If I pack a Java application in an executable jar file that takes arguments from a user, then the user have to invoke the program (jar file) from the command prompt by the following command:
java -jar "jar-file-name.jar"
But I want that whenever a user double clicks on the executable jar file (that needs arguments from the user), a window (command-prompt window) appears that would appear if we had invoked the jar file from the command-prompt.
I know one solution to this is using batch file .bat to run the jar file. Is there any other solution?
To explain why:
There are 2 java JVM exe launchers:
java.exe: console based - provides console input/output.
javaw.exe: for GUI apps - hides the console.
JAR file extensions are associated with javaw.exe by default, which is why you don't get a console when you double-click them.
The answers others have given, and adding my own:
rewrite your app so that it uses Java GUI items for input and output instead of System.in/System.out. This may be over-complex for what you require.
You mentioned creating a batch file so that the console-based Java JVM (java.exe) is run, You could also create a windows shortcut specifying the command line: java -jar jar-file-name.jar
You could change the windows file associations for .jar (but generally this is a bad idea -- new Java installs may reset this, and it will mean all java apps run from jars will have a console)
You could use a Java launcher like WinRun4J which allows you to simply drop a double-clickable EXE with an icon and a config file that specifies how your app should be run (with/without console, and with any other JVM and command line parameters )
Personally I went for the last option in my project - I made my jar file non-executable, and the user has to double-click the EXE. It also allowed me to specify a nice icon for my project, and provide multiple options on launch (debug/non-debug mode) simply by having a different exe/config file.
You would need to reassign the explorer association for ".jar" to java.exe instead of javaw.exe. This is somewhat of a questionable thing to do - it might make more sense to create your console output window. You can, of course, trivially pop up a dialog for user input of required execution parameters if they are not supplied on the command line.
There is working code for a console output JTextArea here.
This is an operating system feature.
If you want full control, then you must provide this functionality inside your program (or with a wrapper class.
Without learning new programing languages, can we using Java get .exe (executable windows file) software directly? And is there away to make .jar (Java ARchive) software transform to.exe (executable windows file)?
Would this conversion effect the performance of the software?
One of the important points of Java is that it'll run on any platform (e.g. Windows, Linux, etc) that has a suitable Java Virtual Machine (JVM) installed. A .exe file is compiled to work on only one platform.
What you think you want is to turn a .jar into an .exe so you can launch it easily. What I expect you really want is just an easy way of launching a Java app that anybody can understand, including your parents.
The easiest way of doing this is to create a Windows batch file that launches your Java app. One of line of script, one new file, one new instruction to your users - "Double click on runme.bat"
There are ways of turning a .jar into an .exe, but you should think about whether that's really what you want, and if so, why.
Note: you can launch a .jar in Windows by just double clicking on it, as long as the main class is specified in the manifest. You might want to look into tools like Apache Ant to simplify building .jars and their manifests. Just a thought.
EDIT:
A batch file in Windows is a simple text file that contains commands. You can test the commands by running them in the command prompt (Start -> Run -> cmd). When you run a batch file the commands in it are just fed to the command prompt one at a time.
How to run a Jar from the command prompt: "java -jar myfile.jar"
If you create a batch file (.bat - use Notepad or your favourite text editor) containing "java -jar myfile.jar" (without the quotes!) then double-clicking it will launch the main class specified in the manifest of myfile.jar. myfile.jar has to be in the same folder as the batch file, though.
If you use the command "java -jar lib\myfile.jar" (again, without the quotes) then your batch file will run myfile.jar, which needs to be in a folder called lib that's in the same folder as the batch file. Use this approach when you have a whole load of Jars with your application and you don't want to shove them in the user's face :)
Note that moving the batch file will break it, unless it uses absolute paths to the jar file - e.g. "java -jar C:\dev\myfile.jar". Of course, if you use absolute paths then moving the Jar will break the batch file anyway :)
Also, note that you should be able to run a Jar file just by doubling clicking on it in Windows, as long as the main class is specified in the manifest. Give it a try. Of course, convincing your users that they can double click on it is another matter entirely...
As a final note, if you use a batch file, your users will get a nice command prompt window sitting in the background until your Java app closes. That is, unless you start your batch file command with "start". E.g. "start java -jar myfile.jar". If you have your app configured to log to System.out or System.err, you will still get a command prompt when your app writes to either of those streams, though.
Final final note, in the Linux world the equivalent of batch files are shell scripts.
You can launch a .exe from java with Runtime.exec() or ProcessBuilder.
You can make a .exe launcher with http://launch4j.sourceforge.net/
Yes, it is possible: http://www.excelsior-usa.com/articles/java-to-exe.html
I'm not sure if this is an appropriate answer because I don't work with Java but could you not using a shortcut file pointing to the java runtime and pass the jar as an argument, this
I realise this is not the only way, nor probably the best way but to me it seams a little better than creating a batch or converting to an exe file if all you're looking for is a convienient way to launch a java app
The very simplest way is to write a windows batch (.bat) file which will start the application.
I was using a wrapper for java to make exe files for a pretty long time:
JStart32
It is just a wrapper for *.jar files.
But ask yourself:
Wouldn't an exe kill the purpose of javas platform independency?
Check out jsmooth (free) and exe4j and you might want to read make your swing app go native