How to load .java file to compiler? - java

Ok, I am beginner in JAVA. I have just started. I downloaded Java SE Development Kit 6u21 and wrote a program, saved it in .java and try to run it, but I can not do it. What's wrong? Thank you.

You will need to compile it first using javac:
javac YourClass.java
And then run:
java YourClass

If you really want to do it manually, you have to use the javac compiler in command line like this :
javac package/of/your/project/YourClass.java
and then
java package.of.your.project.YourClass
Your class YourClass must have a public static void main(String... args) method.
If your class isn't in a package, then javac YourClass.java and java YourClass are sufficient.
You should really consider to use an IDE which will handle this for you.
Resources :
javac documentation
java documentation
On the same topic :
Compiling multiple packages using the command line in Java
Compiling/running a java program in a different directory.

I suggest you read and follow this tutorial by Oracle: "Hello World!" for Microsoft Windows. Once you have successfully done so, you should have JDK installed and know how to run your program.
If you are still getting "'javac' is not recognized as an internal or external command, operable program or batch file", try reading these: How do I set or change the PATH system variable? and PATH and CLASSPATH.

Related

Very first hello world java program works in Eclipse but not in command prompt

I know this question has been answered before but as a total newbie trying to start learning Java i'd be grateful please for a specific answer?
In Eclipse when i press run with the following code it works fine
public class MySweetProgram {
public static void main(String[] args) {
System.out.println("Hello there!");
}
}
However when i go to C:\Program Files (x86)\Java\jre1.8.0_241\bin in a command prompt and type java MySweetProgram i get an error saying cannot load or find main class MySweetProgram
I've searched through other threads that advise to set the classpath. Mine is set to C:\Program Files (x86)\Java\jre1.8.0_241\lib.
I changed the folder in Classpath from lib to bin but got the same error, so i changed it back again
I do a java -version and get the following :
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) Client VM (build 25.241-b07, mixed mode)
Would someone be able to advise me please on what they think may be the issue?
I apologise if this is an ignorant question
Thanks
Jimmy
welcome! Eclipse will be compiling the code for you and then running it. It does this all for you when you click run, to make your life as a developer slightly easier. If you would like to compile and run the command from the command line, you will need to ensure you have access to the jdk (which must be installed somewhere by Eclipse, but may not be immediately obvious where... a folder called /something/jdk/, presumably). With that on the path (in the %PATH% variable), the following should work for you:
cd /to/where/your/code/is
javac MySweetProgram.java
java -cp . MySweetProgram
Have a look into compilation and what it means, so you gain a better understanding of what Eclipse is doing. And good luck to you!
When you compile a .java file, in your case the MySweetProgram.java, you can decide where you want the executable file to go. That file will be called MySweetProgram.class. Eclipse may keep those two files in separate directories.
Normally (before Java11) you could only run a .class file. So you'd have to add classpath (-cp) argument for java to find your file:
java -cp path MySweetProgram (don't put the .class)
Now (after Java11) you can run the java file directly because when you give it a .java file, it will compile it for you just before executing it:
java MySweetProgram.java (if you're in the directory)
java path\MySweetProgram.java
If you have a MySweetProgram.class in the directory, this will give an error.
It gets a little more complicated when you use packages, but Oracle has some very nice Offical Java Tutorials

Running a java.class file from Linux command line - Oracle JDK8 Installed - openJDK removed

So, I have removed openjdk from my new Ubuntu system and have installed Oracle JDK 8 and Eclipse from their respective websites. I can run a program from Eclipse, however I cannot run it from the command line. I am also not used to using Eclipse (I use NetBeans for my Java class in college.) I noticed that there is no build button in Eclipse. With all of that being said, here is my command line code:
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ ls
test.class test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.test.java
Error: Could not find or load main class wiltest.test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.testError: Could not find or load main class wiltest.test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java test
Error: Could not find or load main class test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$
You need to add the -classpath . command line option.
You can learn more about classpaths here
I believe the command java -classpath . test might work, but it really depends on a number of items that are better explained in the link above.
I actually answered part of my question myself. (For anybody wondering, the classpath is set to be in present working directory by default.) Anyways, I was running java wiltest.test from src and not bin. However, I ran from binary file and it worked. BUT I deleted the .class file thinking that I could change the source file, recompile using javac, and it would create another test.class in the binary folder. It did not. -sigh-

java -cp brings up help options?

So, I'm trying to run a compiled class file in command prompt on windows 8 like this-
java -cp C:\Java\MyFirstProgram
However, every time I run it using the -cp option, it brings up a list of several help options, such as defining -cp, -showversion, -X, -splash, etc. I've tried several other ways of typing it as well, however when I don't use the -cp option, it says
Error: Could not find or load main class C:\Java\MyFirstProgram
Other questions on Stack mentioned how I should be using -cp, so I tried that and only the help showed up, which was very.. Unhelpful. Anyone know what I'm doing wrong? And if so, thanks for responding if you do.
Edit: This is the source code
public class MyFirstProgram
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
So how would I include the "class file," then?
The correct usage of using java executable is:
java [-options] class [args...]
where -options include the -cp command option.
You haven't included your class file, therefore it displays to you the usage help.
To run your program, go to your Java folder and run your program.
Example:
cd C:\Java
java MyFirstProgram
I'm guessing the problem is that you need to specify the .class extension:
java -cp C:\Java\MyFirstProgram.class

How to run a simple java class on the command prompt in windows 8

I'm having a bit of trouble trying to run a java class on the command prompt. Its a very simple class and a friend of mine says it could be a problem with windows 8. Are there any suggestions. Here i'll show you the class and how I tried to compile it. I works fine in eclipse.
package gmit;
public class A {
public static void main(String[] args) {
System.out.println("hello");
}
}
In the command prompt I wrote
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>
followed by - javac A.java
java A.class
I tested the class using type A.java
and the text from the class does come up.
What am I doing wrong? Any help would be great.
If it runs in an Eclipse project, but not from command line, you could try this:
C:\Users\eclipse\workspace\Oct1stcasting\bin\gmit>java A
Inside a project directory, Eclipse saves source codes in /src and bytecodes in /bin. Thus, if you simply want to run your bytecode from command line, changing directory to /bin might suffice.
To compile
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>javac A.java
To run
C:\Users\eclipse\workspace\Oct1stcasting\src\gmit>java A
Don't append the .class extension while running the java program
Don’t add a .class suffix when using the java command.
Use java A or java -cp . A. The latter is required if the current directory is not implicitly used as class path.
Compilation:
C:\Users\eclipse\workspace\Oct1stcasting\src>javac gmit/A.java
Executing:
C:\Users\eclipse\workspace\Oct1stcasting\src>java gmit/A

Running a Java Class in Ubuntu with Virtual Box

Since I'm quite new in Java i have a question. As told on the title , I run an Ubuntu Server on VirtualBox and I have a problem running a very simple class with the use of package.
I give you the code:
package world;
public class HelloWorld{
public static void main (String[] args){
System.out.println("Hello World")
}
}
Very simple code indeed. After compiling it with javac HelloWorld.java, with no mistakes (ok now what mistakes could possible find),
Running java HelloWorld, gives me the message NoClassDefFoundError
Running java world.HelloWorld returns cannot find or load main class.
I suspect that it has something to do with classpath , but I cannot find an answer.
It's a classpath issue. You can probably check to see what your classpath is by looking at the CLASSPATH environment variable. You can try adding the directory your classfiles are at to the end of this CLASSPATH, but the simplest thing to do is probably the following.
Make sure the HelloWorld.java file is in a directory called world, and you can compile is like:
javac world/HelloWorld.java
This will create a HelloWorld.class file in the world directory. You can then try running
java world.HelloWorld
or
java -classpath . world.HelloWorld
from the same place.
You can also use the -d flag with javac to put the class files in a different place instead of the same place the source (.java files) are.

Categories

Resources