After exporting jar console doesn't show up - java

I have written a small program in Java in Eclipse, which outputs something in the console using System.out.println() and asks for input. When I export it, and run it, the command line doesn't show up though. It does do something, and when I run it via java -jar [filename] it does show the output in the command prompt. I know it does something because I work with an Access-database, and after opening a connection between the database and Java, there is created a .laccdb file, so something does happen. I don't see anything though.
I hope it is clear what I mean and I also hope someone can help me!

Apparently, when double-clicking on a .jar-file that only outputs in the command line will not show the command line. The only way to accomplish this is by running the jar with java -jar [filename].

Related

Why is the jar file immediately terminated after being called using a Python script?

I'm currently programming a Python script that opens a file in an different directory that the user enters in an input box. Everything runs fine but the jar file I used for testing doesn't run or show up. I'm using the Shimeji-ee.jar in testing, and at one point its tray icon showed up but disappeared immediately.
I tried running it in command prompt(since calling the jar file using the script is similar to running a file in CMD) and discovered that it only runs as long as the CMD window is open. A few searches later, I've found a way to run files in CMD that keeps it running even after closing the CMD. I wrote it in my script, no errors, but the Shimeji nor its tray icon doesn't ever appear anymore.
I've added a line at the end of my script that is also told to be an efficient way of keeping the script running, but it doesn't work either(could be another mistake here):
while True:
keyboard.wait('q')
if keyboard.is_pressed:
sys.exit()
Here's the line of code in my script that does the calling:
subprocess.run(['D:', 'cd', PurePath(fileDirectory), 'START', '""', fileToExecute], cwd=os.getcwd(), shell=True)
The code I learnt that makes a file run in the background(similar to adding & in a Linux terminal):
START "" program
I've had thoughts that the jar file I'm using could be the problem, but I haven't found any answers for hours. Is there anything wrong with the code or am I missing something?
Update:
Code finally worked after the first answer but I received an error that seemed it read the file as a double forward slash like this:
# The network path was not found //
or
# The system could not find the file //
The solution I found was just removing the '""' part of the code, which makes the code look like this:
subprocess.run(['START', fileToExecute], cwd=PurePath(fileDirectory), shell=True)
The problem is that your code:
subprocess.run(['D:', 'cd', PurePath(fileDirectory), 'START', '""', fileToExecute], cwd=os.getcwd(), shell=True)
executes the command D: with the arguments cd somedirectory START "" fileToExecute, which changes the current drive of the shell to the D drive and then terminates.
You probably want to execute
subprocess.run(['START', '""', fileToExecute], cwd=PurePath(fileDirectory), shell=True)

How to keep the cmd open?

When we click on .jar files which do not contain any GUI the CMD Prompt runs the code and exits it immediately.
How to make it remain open when running a .jar by double clicking on it?
Is there any java code for it? Just like the pause command in batch files or any? I prefer only by java code but not an OS way? and also not by running command java - jar *.jar in cmd prompt.
I'd usually just open a command line first, as suggested in the other answer.
If you need a pure Java code solution though, just read from system in at the end of your code. The window will stay open while waiting for input.
Run the jar file from the command prompt
java -jar yourjarfilename.jar
Something along the lines read a character from std-in along with a print statement telling people to "press any key" should do the trick, provided you have access to the java code that is.
if not well, best option is to drop to CMD and run the command manually.

javac and java commands through Windows batch files

I normally compile things through the command line using:
javac -classpath . Test.java
Similarly, I run them through:
java -classpath . Test
I'm now attempting to save myself the trouble of typing these out every time through batch files. I have attempted to do so through another question from here:
Creating a batch file, for simple javac and java command execution
I've also tried my own way:
cmd.exe
#echo off
javac -classpath . Test.java
Still no luck, however. I have checked that my PATH environment variable is correctly pointing to the latest version of jdk and as I've said, I can compile just fine directly through command line. Upon running the batch file, I just get the command prompt with no error; as if there was nothing under cmd.exe. Could anyone lend a helping hand and slap some sense into me?
When you write cmd.exe, that will start a new command prompt. You don't want that.
When you write #echo off, that means nothing will be printed on the screen after that point. That's what it means. That's what it does. That is why it looks like nothing is happening.
Something would be printed to the screen if you had a compilation error, but probably you don't.
If you want the command prompt window to stay around instead of disappearing, I believe there is an option in Windows to configure that, at least there was when I last used Windows, back in the mists of time.
this worked for me. I think it does what you were looking to do.
This is the code I suggest for the .bat file:
cd C:\Users\John\JavaApps\folderThatContains.java //points terminal to folder
javac Main.java //This compiles .java in said folder
cmd /K "java Main" //cmd /K prevents terminal from quitting after "java Main"

Java - System.out.println() viewable from application?

I have a Java application that runs great :) While uploading files, it uses the standard output to show progress : "System.out.println(...);".
When I run it in Eclipse, well it works perfectly, but when I run the JAR file, I don't see any console/terminal showing up and printing what I print through "System.out.println(...),".
How can I open a new terminal when my application is launched (it is a Swing application)?
Basically I want to be able to run the Swing application and show information on the side in a terminal / console. Why? Don't worry about why I want to do this ;)
Thanks a lot!
Regards.
Open terminal and run application as java -cp yourjar.jar YouMain or java -jar yourjar.jar if you jar is runnable.
I believe that you do not see output because you are running your application using javaw - the special windows-only variation of JVM that does not have STDOUT at all. If you want to click your application and see output map *.jar file to be opened using java instead of javaw. Alternatively write bat file that runs your application. In this case you will see console.
Use java instead of javaw to launch your application. Double-clicking on a jar executes it with javaw. Instead, open a command line window and type
java -jar thePathOfTheJarFile.jar
If you want to have something double-clickable, then write a shell script containing this command, and double-click the shell script instead of the jar.

Java programme works perfectly in NetBeans, but exits unexpectedly when trying to run

I've run into a weird problem today. I'll try to describe the steps I'm making as detailed as possible.
I've created a Java application in NetBeans. The application runs without exceptions when running within NetBeans by clicking Run Main Project. The total duration of a complete run is about 1min.
When building the project I see the following option in the output window of NetBeans:
To run this application from the command line without Ant, try:
java -jar "C:\java_libraries\PrxJobCommunicator\dist\PrxJobCommunicator.jar"
So i open the command prompt (Windows 7), enter this command + supply a mandatory integer argument and press enter
The application starts running, runs for a couple of seconds and exits without any message whatsoever.
the application writes to a text file at different points in the code, so I'm able to trace the output up to the point when it stops running. I've deliberately put some extra calls to the file writer method in order to find where the programme continues/exits but to no avail. Seemingly the programme simply stops running on a certain line and that's it.
I really don't know what the problem can be: the application runs fine in NetBeans and fails without any message whatsoever when starting from the command line.
Shall I check anything special in NetBeans? Do you know of any special technique to debug a jar file if it's started from the command line?
Any clues are welcome.
Thanks, Andras
I suspect that there is a difference in the classpath when the app is run from NetBeans, and when it is run from the command line.
There must be some kind of exception that is being thrown, have you got an outermost
catch (Exception ) block in the main method?
You could try to debug the application using "remote debugging", although you won't be debugging over a network, you can still use this method to catch the breakpoint started at the command line in NetBeans.
Here's an article that explains how to do remote debugging with NetBeans http://manikandanmv.wordpress.com/2009/09/24/debugging-java-applications-with-netbeans/
Netbeans by default uses your project folder as the working folder. It means when you run your program from console you have to CD C:\java_libraries\PrxJobCommunicator
then run java -jar "C:\java_libraries\PrxJobCommunicator\dist\PrxJobCommunicator.jar"
If above fails check the java version used in your command prompt. java -version
It has to be same as the one used by netbeans.
Finally the library path. Are all the dependent jars placed in the dist folder?

Categories

Resources