I got a strange problem in my windows 7 system running jdk1.6.0_33
When I try to run a simple java program from command prompt, it opens a new window (something like java frame) and suddenly disappears. There is no result shown on command prompt also I am unable to terminate the process (using Ctrl+C) or close command prompt after this. A java process is created each time I do this. I tried to kill process using Task Manager, but that too didn't work.
I am able to run the same program using eclipse.
Here is my program
class HelloWorld{
public static void main(String [] args)
{
System.out.println("Hello World");
}
}
Environment variables are set as follows:
Path=C:\Program Files\Java\jdk1.6.0_33\bin
classpath=.
Commands I used are,
javac HelloWorld.java
java HelloWorld
Why is this happening? Thanks in Advance.
I'm not sure, but I think this is what happens if you use java versus javaw from the command prompt on Windows:
References:
The java command manual page for Windows.
Difference between java/javaw/javaws
If your java class path is configured correctly.
type javac command in command prompt
You will get java compiler information.
After compile the class find your folder location if any .class file is created.
Related
I'm a beginner trying to learn to program in Java. I'm using the book Java All In One for Dummies. The book has me using Textpad to write and compile the code. I've got the following simple program:
public class HelloApp {
public static void main(String[] args) {
printHello("World");
}
public static void printHello(String greetee) {
System.out.println("Hello " + greetee);
}
}
I saved the files for the java application in at the following path. D:\program files\java. When I compile the app with the Textpad compiler, it compiles without any errors. I have the files saved in the following folder: D:\program files\java. When I try to run the app from the Run Java Application command in Textpad, it gives me the error message "Error: Could not find or load main class HelloApp.java". When I run the app from the command prompt using the java HelloApp.java, it runs fine displaying "Hello World" Why does it work when I run it using the java HelloApp.java command which is what I assume the "run java application" command in Textpad does, and not work using the command in Textpad? What am I doing wrong?
I tried resaving and recompiling the application. Then I ran it directly from the command prompt. It worked when running directly from the command prompt but it won't work when trying to run from the "Run Java Application" command in Textpad.
I assumed it would work as it seems like I'm doing the exact same thing from Textpad as I am from using the java HelloApp.java command at the windows command prompt.
Thank you for your replays.
I'm writing this code using eclipse on ububntu OS.
this is my code:
public class test{
public static void main (String[] args){
System.out.print("Hello world");
}
}
Then make it by using eclipse export JAR Executable file.
I run the code under ubuntu terminal like this:
java -jar test.jar
so I got the Hello world string, and this is the result:
Screen shot on ubuntu
And running the same command on the command prompt of windwos 8.1 x86.
java -jar test.jar
And the result is this: Result on windows
Thank you again for helping.
This might be the classpath issue , use below code to run. -verbose will print you the execution details.
set classpath=.;
java -verbose -jar helloworld.jar
Please show us your code and the way you run it.
First idea: make sure you run your code on the command line:
open DOS box
cd to the directory where your .jar file is
java -jar helloworld.jar
You might not see any console output if you double-click the .jar file
This is a simple Hello World test application.
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
try {
System.in.read();
} catch (IOException e) {
System.out.println("IOException : "+e.getMessage());
}
}
}
It was exported as "Runnable JAR File" in Eclipse.
Opening it using java -jar hello.jar on Command Prompt works.
However when trying to open it using double click, nothing happens.
Note 1 : Java Platform is the default program associated with this type.
Note 2 : java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
The problem Double click does not execute a command line JAVA 8 jar file.
How can i solve it?
If you doubleclick a *.jar it is actually started with javaw -jar Hello.jar. The difference between java.exe and javaw.exe is that with javaw there is no associated console window. As your test application do not show any GUI element, it looks like as it would not be executed.
Depending what you want to achieve you either create a script to run your application or you make some graphical interface.
When you execute the JAR from File Explorer, there's no shell for it to write the text output to. When you run it from the command line, it's already wrapped in a shell so the System.out stream is already set to the shell's output stream and so print statements are visible.
The simplest practical solution would be to create a batch file that performs the java -jar hello.jar command which would launch a command window when you open it in File Explorer.
Another solution would be to launch a cmd.exe context from within your Java application using Runtime.exec() and set your output stream to the same as the process object it returns.
The most elegant solution, but one which requires a bit more background work, would be to instantiate a simple GUI consisting of a JFrame and a JTextArea within a JScrollPane, then you would append your output to the text area.
Your jar may be opening with javaw.exe. You can verify that by executing command
ftype | find "jarfile"
If its showing as javaw.exe
ftype jarfile="C:\Program Files\Java\jdk1.8.0_45\jre\bin\javaw.exe" -jar "%1" %*
change it to java.exe
ftype jarfile="C:\Program Files\Java\jdk1.8.0_45\jre\bin\java.exe" -jar "%1" %*
I tried and it worked for me.
I'm getting started with java development. So, I installed JRE and JDK in my computer.
Then, I created a simple Example.java file and saved it in my Desktop.
In the prompt, I executed
javac Example.java
and it worked ok. A .class file has been created in my Desktop.
Then, I tried to run the class, executing this:
java Example
and I got an error in a window alert, with this message:
Java Installation Not Completed
Unable to install Java
There are error in the command line switches: "Example";.
Check that the commands are valid and try again
Then, for testing, I executed both commands:
javac -version and
java -version. Both are installed in my computer.
What am I doing wrong?
I am running Windows 8 and have already set my environment variables.
Example.java:
public class Example {
public static void main(String args[]) {
System.out.println("Finally Java");
}
}
Try to remove installation again, look for all leftovers and remove them manually, if you changed the directory you installed java to, remove environment variables as well and set them again. You should also make registry cleanup: https://java.com/en/download/help/manual_regedit.xml Make installation through offline installer.
Just today I noticed that I can run java in eclipse with no problems but when I try to run it in the command prompt, I get "cannot find or load main class." The command prompt actually compiles all right, and it outputs a .class file, but then it displays the error msg when trying to execute. (Also, I was able to run java in the cmd a couple weeks ago.)
/* work area for practice
*
*/
package Scrap;
public class experimentational {
public static void main (String [] args) {
System.out.println("welcome to java!");
}
}
Found the answer: (i'm using different code but it is still relevant to this problem)
java -cp . hiThere
output: "Hi there"
I know this is classpath but don't know why it works or what the period does for it. Anyone have an idea?
Use:
javac Scrap/experimentational.java
followed by:
java Scrap.experimentational
try java -cp . [your main class].
Did you install a JDK on the machine outside of Eclipse? If you did, then make sure you set your path variables correctly. Open a command prompt (assuming windows) and type java -version
If the JDK was installed properly and path variables were set properly it should tell you the version of Java that was installed. If it tells you that 'java' is not recognized as a command that you do not have a JDK installed, or it was not installed properly.
The reason your program runs in Eclipse is that Eclipse for Java has its own internal JDK and JVM.
Your other option is to set up your path variables to point to Eclispe's internal JDK.
If you were able to run it from a command prompt previously then most likely your class path was altered. Is this a machine at work? Some companies have SMS tasks that come through periodically and restore default system settings (including path variables) to corporate defaults.
Maybe java and javac isn't in your OS path.
If you are using Microsoft Windows in cmd type path and then enter.
If jdk or jre isn't in path you need to put them to it
I had a similar issue when I copy pasted code into an editor. I removed the package declaration on line 1 and it ran then. So I'd investigate above comments on packages, after trying first to remove the package line.