Why does a jar file work differently with and without console? - java

I obtain different results using the same program when I run it thru:
1)..\Desktop\app.jar
2)..\Desktop\java -jar app.jar
second case gives the same result that I saw in eclipse but it uses console.
Question is: How do I force my program to work properly (to give the same result as in eclipse) by direct executing app.jar (without console)?

Use javaw instead of java in command line:
..\Desktop\javaw -jar app.jar
More information: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/java.html

Related

.jar file is not working after being published from Eclipse [duplicate]

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.

Jar file execution

Suppose I made a program to take two input and display the sum.
It runs in netbeans but when I build out the jar file , and
double clicked the jar file , nothing happens.
How can I make these program run?
You must run this from command line, and then execute java -jar {PATH_TO_JAR}
You can use the command-line :
java -jar <yourjar> <yourMainClass>
You can also add the main class in a manifest file into the jar. Doing so, you don't need to specify anymore on command line
Jar files needs to run in JVM - Java Virtual machine, to run your file use:
"java -jar" and your jar.
By doing so, you tell Java to run your jar in jvm which means it now can run.

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: System.out.println() should write to the console, but it doesn't

I am trying to write Java console application, the code is quite simple:
public class ConsoleTest {
public static void main(String[] args) {
System.out.println("test");
}
}
If I run this application from Eclipse, then i see "test" in Eclipse's "Console", but if I export my app as a "Runnable JAR file" and run it from Windows XP cmd.exe, then nothing is echoed to the console.
On the safe side, i tried to check System.console(), it returns null.
What can be wrong?
How are you running your program outside eclipse?
You should use command line like
java -jar yourjar.jar
or
java -cp yourjar.jar ConsoleTest
If you are occasionally using javaw instead no console output will be produced. The STDOUT of javaw is null. Probably this is what happens when you are clicking on your jar file.
You need to run your jar file from the command line.
If you double click on it, you wont be able to see the command line operations being run in the background. Jar files are usually run by double clicking only when they involve GUI.
To run a jar file from a command prompt, just do this:
java -jar ConsoleTest.jar
Assuming you've set environment variables for java.exe and the current directory has the jar file.
If this doesn't work, then it is likely not your code's fault.
There is also the chance that the manifest file pointing to the Main class was set up incorrectly.
Try compiling it into a class from the command line and then running it. Does that work?
javac ConsoleTest.java
java ConsoleTest
Chances are your runnable jar file isn't working as you expect and isn't actually running your ConsoleTest class.
Like what Hovercraft Full Of Eels wrote, compile the .java file using cmd.exe with the following command:
javac ConsoleTest.java
Then this will create a .class file, and we want to compile it with this command:
java ConsoleTest
It should then display the "test" output.
you have to install the java jdk and add the root of the directory containing the file java.exe to your system's directories, you will find further informations in the jdk's installation guide.
and then run the file in the console with the command
> java file.java

How do I run a java program from a different directory?

I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin

Categories

Resources