java -cp brings up help options? - java

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

Related

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

NoClassDeffFoundError when running java.exe from different directory than .class files

I'm trying to learn to run java apps from windows command line and I can't figure out one problem.
I have a simple class on my desktop:
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
If I run javac and java commands when I'm in my desktop directory in cmd everything is well, but if I go one directory back (so I won't be in the same directory as the .java and .class files) then my cmd directory is C:\Users\Tomas and my Hello.java and Hello.class files are in C:\Users\Tomas\Desktop. I can run the command javac Desktop\Hello.java and it works, but then if I try to do java Desktop\Hello.java I get an Exception in thread "main" java.lang.NoClassDefFoundError: Desktop\Hello (wrong name: Hello).
I know that NoClassDefFoundError is throws when a class was available at compile time, but ClassLoader can't find it during run time ( found a good article about it here).
I think the problem has something to do with the CLASSPATH variable, so I set it to:
"C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop"
(I included "C:\Users\Tomas\Desktop" just to try everything)
And I tried running the "java" command with -classpath and -cp options:
java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Desktop\Hello
And I keep getting the same exception.
I't would be great if someone can explain my error and why this is happening, and maybe point even give some directions where can I read more about this.
Thank you.
class
package Desktop;
public class Hello{
public static void main(String[] args){
System.out.println("1, two, three");
}
}
compile (here Desktop means standart windows directory)
javac Desktop\Hello.java
execute (here Desktop means package. Desktop/Hello is fully class name)
java Desktop/Hello
java -classpath 'C:\Users\Tomas\Desktop\Hello.class'
Should run it.
Try java -classpath "C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext";.;"C:\Program Files\Java\jdk1.7.0_21\jre\bin";"C:\Users\Tomas\Desktop" Hello
I only removed Desktop from your class name.

How to execute my HelloWorld script

I'm currently trying to run my first java script:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I decided i'd take a little look into Java. However I come from languages like JavaScript and PHP which don't require any compiling or anything as such.
So far, I think i'm compiling it correctly in the command prompt:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" "HelloWorld.java"
It adds a file called: HelloWorld.class so figured I did something right.
However, now when I try to actually run the program using:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" "C:\Users\Shawn\HelloWorld.class"
I get this, Error: Could not find or load main class C:\Users\Shawn\HelloWorld.class.
However, if I try that same command but use javac.exe instead I get:
javac: invalid flag: C:\Users\Shawn\HelloWorld.class
Usage: javac <options> <source files>
use -help for a list of possible options
Why is this happening? Why isn't my program executing correctly?
The java command takes the name of the class, not the name of the file.
It then uses the Java class loader to find the .class file for that class in the current directory or the class path.
When you pass HelloWorld.class, it looks for a class named class in the package HelloWorld.
(that would be ./HelloWorld/class.class)
You need to pass HelloWorld.
As others have pointed out the argument needs to be just the class name, without the extension .class. There is a second requirement that must be met, though: the class file must be in the classpath.
It's usually not advisable (although convenient) to include the current directory in the global classpath, but you can override it on the command line:
java -cp . HelloWorld
You can also specify an explicit path:
java -cp "C:\Users\Shawn" HelloWorld
If your Java program uses other classes, include the global classpath like this:
java -cp "%CLASSPATH%;." HelloWorld
See the following javac documentation for more info. especially the section on Cross-Compilation Options.

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.

How to load .java file to compiler?

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.

Categories

Resources