I'm learning java and I would like to know how to compile a java program from other directory.
For example, my compiler is in my drive c:\ and I want to compile my java program from drive e:\
How should I do that?
I'm getting this error, what does it mean?
The current directory should be in the default CLASSPATH, but maybe it's not. Try java -cp . Assignment
It's been a while since I've done java, but it seems like the compiling isn't your problem. Since javac returns properly, it seems to be a problem with Assignment.java. Does your Assignment class have a main method?
Well the easy way is to set ur classpath variable. Since from screen shot it seems ur using windows i suggest u right click the my computer nd select properties. Go to advance setting and click environment variable tab.
Then a new window pops up which has System Variable at bottom. Select new and create a variable
JAVA_HOME = path where u installed java eg for me its c:\java
Now once u add this search an existing variable path and choose edit. Now at the end append the following ;%JAVA_HOME%\bin
Now ur done u cn run java program frm any location on ya comp....
You are using package in your code...so it shows NoClassDefFoundError when you run
you should create folder which contain your package name...compile that java file...and you can run that file from previous directory of that java file directory...
For example your code is
package test;
class Assignment{
public static void main(String args[]){
System.out.println("Hai");
}
}
it saved on this path "E:\java\test"
compile this file and you can run this file from this path "E:\java"
command to run this file
java test.Assignment
E:\java> java test.Assignment
Is there a package declaration at the top of your Assignment.java? If so, remove it and recompile for a quick fix.
To work with Java packages, you'll need a directory structure that matches the package declarations.
For example, say this is your Assignment.java:
package myjava;
public class Assignment {
public static void main(String[] args) {
....
....
}
You run this command to compile:
E:\java>javac -d . Assignment.java
And you get myjava\Assignment.class if all went well. The -d . option means "place generated class files in the current directory". javac creates the package hierarchy as directories for you.
Now that your directories match your packages, this should work:
E:\java>java myjava.Assignment
Related
So I have a basic hello world set up in eclipse and I can compile it using cmd easily (I have set all the necessary paths), however when I then try to use the java command to execute the hello world, it always returns the same error:
Error: Could not find or load main class helloWorld
Caused by: java.lang.NoClassDefFoundError: net/codejava/helloWorld (wrong name: helloWorld)
This is the code used:
package net.codejava;
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I am cd in the right directory (I think, I cd into the src directory and then into the package file stored in src) and am using Windows 10 with java 18.0.1 and JRE build 18.0.1+10-24
Any help would be greatly appreciated, as this is highly frustrating, when the code runs effortlessly on the eclipse console. Thanks.
Your file has a 'package' of net.codejava and a name of helloWorld, meaning, the full name of this class is net.codejava.helloWorld.
The java command, at least in the way you're using it, requires that you pass the full name, thus, you must run java net.codejava.helloWorld. Just java helloWorld simply isn't going to work.
But that's not all.
Java needs to then find the class file that contains the code for class net.codejava.helloWorld. It does this by first turning that full name into a path of sorts: net/codejava/helloWorld.class, and it will then scan each entry in the classpath for that. You can put directories and jar files on the classpath.
Thus, you have a directory on your system; let's call this directory X. X contains a directory named net, which contains a directory named codejava, which contains a file named helloWorld.class. If there is no such X (i.e. your class file is not in a dir named codejava for example), you're going to have to fix that by making these directories.
Then, X (and not the codejava dir!) needs to be on the classpath. Usually (it depends on how you configured things), 'the current dir' is by default on the classpath.
Given that your code is in, say, /home/PythonSux/workspace/learningjava/net/codejava/helloWorld.class, that means the dir that needs to be on the classpath is /home/PythonSux/workspace/learningjava. After all, if you, from there, look for net/codejava/helloWorld.class, you find the right file.
Therefore, either cd to that directory, or run java -cp /home/PythonSux/workspace/learningjava net.codejava.helloWorld
Note that this isn't usually how you actually run java apps. You either run them from your IDE, or you ask your build tool to run it, or you package your java app into a jar file and run that, etcetera.
I am trying to compile and run some java files I have made in Eclipse. The full path to the .java file is C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java. tests is the package I created in Eclipse and src is a folder that Eclipse made under Introduction (which is the project name).
In my environment variables, I have the following relevant variable:
JAVA_HOME C:\Program Files (x86)\Java\jdk1.7.0_40\bin
Under system variables I have the following:
CLASSPATH %JAVA_HOME%
I go to my cmd and cd into the tests directory (cd C:\Users\MYNAME\Documents\Java\Introduction\src\tests). Then I compile using javac Test.java. This seems to work as I then have a Test.class file under the same directory. Now I want to run the file, I type java Test and I get the error, "could not find or load main class". I've tried a variety of things including appending .class and .java to the end but I keep getting the error. I looked at some answers and docs and I managed to get it to work if I cd into:
cd C:\Users\MYNAME\Documents\Java\Introduction\src (i.e, get out of the package)
and then run:
java -cp . tests.Test
So that seems to temporarily set the class path to the current directory, and run Test from the package tests. However, I want to simply be able to type java Test. I know it's possible as I used to be able to do it, but now for some reason I cannot (I must have changed something along the way...).
Any help is appreciated.
However, I want to simply be able to type java Test
That will only work if Test is in the default package - it's as simple as that. You need to pass the java executable the fully-qualified name of the class you want to launch. There's no way round that.
Of course, you could create your own launcher which looks in the current directory for class files, finds out the fully-qualified name of the classes within those files, and launches java providing the full name and probably specifying an appropriate classpath... but that seems like a lot of hassle compared with just including the package name in the command.
You could be making the same mistake I made. So, try the following.
Here is my code for your reference.
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Once you saved this as "C:\JavaStudy\ClassA.java", try the following.
c:\JavaStudy>javac ClassA.java
c:\JavaStudy>java A.class
Error: Could not find or load main class A.class
c:\JavaStudy>java A
Hello world
c:\JavaStudy>
Note: You don't need to use " java.exe -cp . " if you have class file in the same directory from where you are executing.
I looked around, but none of the solutions that worked for others worked for me.
I installed java 1.8.0
My path variable is C:\Program Files\Java\jdk1.8.0_05\bin
I try to run the following program hello.java:
package hello;
public class hello{
public static void main(String[] args){
System.out.println("Hello");
}
}
The program compiles fine when I run javac hello.java
But when I use java hello , java -cp . hello, or java -classpath . hello it returns the error 'Could not find main class hello'.
I know this is a very basic problem, but I really can't figure it out.
Thanks in advance
In very similar answers that I've provided, provided that you're compiling in the current directory, then you need to ensure that your compiled class has made its way to a folder called hello/.
If it has, then you can run this:
java -cp /path/to/hello hello.hello
The above adds the hello/ folder to the classpath, and then you can run the main class using its fully qualified name.
Here you define package so you cannot run directly your compiled class because JVM is not able to find your class so you have to write the path of your hello directory in run command.
for example:
java -cp c/workspace/hello hello
I try your code. And see what the problem is. If you put the "hello.java" in a folder named "JavaTrials" and compiled it there as "javac hello.java", it compiles and produces "hello.class" right there. This command does not produce a folder named "hello" for the package.
Your compiled code "hello.class" should be in a folder named "hello" which is the name of the package. And then you have to run the command "java hello.hello" not from inside the folder "hello" but from the containing folder.
The better way is to put your code in a folder named "hello" before compiling it. This folder represents the package. Then compile it from the outside of the "hello" folder with command "javac hello/hello.java". And then you can run it by "java hello.hello"
As a side note, in java coding tradition class names begins with upper case. Se better use "Hello" instead of "hello".
remove first line "package hello;"
Java and Gradle beginner's question.
I made a project directory for java and gradle test:
The directory hierarchy :
HelloWorld.java:
package foo.bar;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
build.gradle:
apply plugin:'java'
Then,gradle build this project and generated what i need.
As you see above, my problem is why doesn't this execute correctly? Even through I cd to .class path.
======================================================================
While, if I remove package foo.bar; in HelloWorld.java, and repeat gradle commands and execute at he.bak directory then the error remained the same.
But when I cd to the directory where HelloWorld.java placed. everything goes OK!Why? something related with CLASSPATH environment variables or other causes?
////////////////////////////////////////////////////////////////////
UPDATE
////////////////////////////////////////////////////////////////////
Thought you guys' warm replies, I know that I should combine the CLASSPATH and the period-separated executable .class file to figure out what's going on when executing java class file.
I experiment my thought resulting in 2 point to this question:
The -cp option path parameter A/B plus the executable file c.d.e.class finally form the A/B/c.d.e.class full path where the class is actually located.
If I specify the package in source code file with package d,I must split the full path in the form of java -cp A/B/c/d e.class. split in other ways all will result in errors.
something I am not sure here is :
When I specify my package path in my source code file, It determined the only classpath when executing corresponding executable, right?
If it is the truth, How does a project with lots of package and sources files work?
What's the root principle?
When in build/classes/main try java foo.bar.HelloWorld instead of java HelloWorld
The reason you need to specify foo.bar.HelloWorld is because you specified package foo.bar;. This tells java that the class should be in foo/bar/HelloWorld and the fully qualified name for HelloWorld is foo.bar.HelloWorld. If you want to execute the class from a different working directory however, you can specify the classpath explicitly using the -cp option, e.g., java -cp c:\myproject\build\classes\main foo.bar.HelloWorld.
By the way, the classpath default is the current working directory (i.e., .) but java -cp c:\myproject\build\classes\main foo.bar.HelloWorld will NOT have the classpath set to the current working directory if it is explicitly set using the -cp option. If you want to include the current working directory but explicitly set it, or even add more directories, you can chain them using semicolons like this: java -cp .;c:\myproject\build\classes\main foo.bar.HelloWorld. So this will include both the current working directory and the directory I specified.
I installed the J2SE 6.o version. Now I'm having a problem getting it to work right.
> C:\java\jdk1.6.0_25\bin
This is the path of the bin file, and I put this in the Path tab. In the environment settings.
What are the next steps that I have to take to run .java files from the command prompt?
Do I have to put something in the class-path tab too?
Let me elaborate my problem:
If I run and compile the below mentioned file called Shirt.java it works fine.
public class Shirt{
public int ShirtID=0;
public String description="-description required-";
public char colorCode='U';
public double price=0.0;
public int quantityInStock=0;
public void displayShirtInformation(){
System.out.println("ShirtId:"+ShirtID);
System.out.println("ShirtDescription"+description);
System.out.println("Color Code:"+colorCode);
System.out.println("Shirt Price"+price);
System.out.println("Quantity In Stock"+quantityInStock);
}
}
But if I run another file that calls the previous file, then problems crop up.
The file that calls the previous file is as follows.
public class ShirtTest {
public static void main (String args[]) {
Shirt myShirt = new Shirt();
myShirt.displayShirtInformation();
}
}
When I try to execute the second file, there are a few errors that crop up and no compilation takes place. I believe it has something to do with some problem with the environment variable Path declaration.
It is better to make sure that you do not have a CLASSPATH environment variable set. If it is not set, Java will by default look in the current directory for class files. As long as your Java source files are in the same directory (and not in a package) you should be able to compile and run them with simple commands:
javac Shirt.java
javac ShirtTest.java
java ShirtTest
If this complains with a NoClassDefFoundError, then try specifying the classpath on the command line using the -cp option:
javac -cp . Shirt.java
javac -cp . ShirtTest.java
java -cp . ShirtTest
(note that . means "the current directory").
See the Getting Started tutorial, which also has a section on common problems and their solutions.
When you get an error, please always copy & paste the exact error message, instead of just saying "I get some errors". The more specific information you give, the easier it is to understand what the exact problem it is and the better we can help you.
I would use an IDE, this avoid the need to
setup the path
check that all the classes you need have been compiled.
setup the classpath for the java
Instead all you need to do is hit the Run button and it does the rest.
It may even help you write/format the code and generate a toString() method, getters/setters and unit tests for it.
There can't be any problem with the path settings as the first java file is working, otherwise it would have given "'java' is not recognized as an internal or external command" error.