Problem While Setting up JAVA fro the 1st time on Windows 7 - java

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.

Related

Java runs in eclipse and will compile, but wont execute on cmd, but still runs in eclipse. How can I get it to execute in cmd?

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.

Error; Could not find or load main class (Java using Windows CMD)

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.

Using ClearNLP Semantic Role Labeler

I want to use clearNLP (http://clearnlp.wikispaces.com/) for extracting semantic role labels of an input sentence. I followed the instructions here: http://clearnlp.wikispaces.com/installation (I downloaded the jar files, put them in a directory called ClearNLP and set the classpath) but when I run the command java com.clearnlp.run.Version, I face the error: Could Not find or Load Main.
I tried it twice: Once I set the classpath as an environment variable of windows and ran the command in CMD. But, when it didn't work, I tried to create a java project, set the libraries using NetBeans and run the program. But, it didn't work, too.
BTW, when I run echo %classpath% command, I see that the classpath is set correctly.
Can anybody help me?
Try Eclipse. I included the jars in a new project I created. I then created a simple class like so
package test;
import com.clearnlp.run.Version;
public class TestClearNLP {
public static void main String(args[]) {
Version.main(args);
}
}
When run, this creates output in the console of:
ClearNLP version 2.0.2
Webpage: clearnlp.com
Owner : Jinho D. Choi
Contact: support#clearnlp.com
The only weird situation I ran into was that Eclipse did not like the jar files beginning with a period. I removed those from my project and ran with the remaining libraries.

rmic error class not found

I'm reading head first java and I'm about to try the ready baked codes that shows how to use RMI.
These are the classes:
The remote interface
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
The remote implementation
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String sayHello() {
return "Server Says,Hello";
}
public MyRemoteImpl() throws RemoteException { }
public static void main(String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Then I placed the .java and the .class file in c:\RMI. When running it, it says MyRemoteImpl class not found even though I'm running from the same directory. How can i fix this? Thanks.
EDIT: The error appear when I try to run this command
rmic MyRemoteImpl
The comments of BuddingProgrammer worked for my as well.
For instance if your classes are in folder C:\users\renato\workspace\ADI\src\semana5 you should go to one level above:
C:\users\renato\workspace\ADI\src and you should compile like this: javac semana5\Calculadora.java
To run RMI you should type the following command in C:\users\renato\workspace\ADI\src:
rmic semana5.CalculadoraImp
Create a folder name it HelloServer
Add a this package package HelloServer; on the top of each class, MyRemote and MyRemoteImpl.
Open cmd and write javac HelloServer/MyRemote.java and javac HelloServer/MyRemoteImpl.java from the directory that contain the HelloServer folder.
Write rmic HelloServer.MyRemoteImpl from the directory that contain the HelloServer folder.
You should have now a MyRemoteImpl_stub.class and you can have a nice day :)
PS: It is important that the package name is different than RMI or any object used inside the class. Otherwise you will have object collision.
Since I had the same problem and none of these answers helped me, I'll add what worked for me. I was running rmic MyRemoteImpl in the directory that contained MyRemoteImpl.java and got the same error. When I ran rmic MyRemoteImpl in the directory that contained MyRemoteImpl.class, it worked and created MyRemoteImpl_Stub.class. I did not need to set CLASSPATH.
if the class is located in C:/../src/Assign/implement.class then
change your directory in cmd by entering:- cd C:/....../src
run the command:- rmic Assign.implement.class
It worked for me
If you're not running this command from the directory containing the .class file, you should be, assuming there is no package statement as per what you posted. If there's a package statement you should be running this command from the directory that contains the highest-level package.
Otherwise there's something wrong with your CLASSPATH environment variable. It should at least contain ".", probably some other stuff as well. As a workaround you can adopt Isaac's suggestion but then you'll only get the same problem when you come to compile and execute.
I have the same code, but in a Maven project. First step is to compile all the code with mvn compile. Note that all compiled classes (ex.MyRemoteImpl in this case) are stored in the target folder in your maven project.
The next step is to cd to \target\classes folder.
Finally, run the rmic command with the fully qualified name of the target class, i.e with the packages of the class - rmic my_package1.my_sub_package.MyRemoteImpl.
I know I'm about 8 years too late, but just for anyone else in the future who might come across this same problem, the following worked for me:
In the folder that contains your files, first start off with javac *.java and then proceed with the rmic class_name step. This should generate the stub.
There is a path variable called CLASSPATH where the JVM will look for class files to excute when you try to run a class file.
CLASSPATH describes the location where all the required files are available which are used in the application.
Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate the required files.
If the CLASSPATH is not set, Java Compiler will not be able to find the required files and hence will throw error.
If you don't know where your current path is, then type echo "%CLASSPATH%" in the command prompt.
Alternatively, you can also add/remove/edit the CLASSPATH variable in the Environmental Variables.
Make sure that you have added your Java to your path.
To add the current path where your .class files exists there are three ways.
Using Environmental variables - Reference
Using Command Prompt - set PATH = .
Specify the class path using -classpath argument while executing.
Semi-colon (;) is used as a separator and dot (.) is the default value of CLASSPATH while specifying the class path using -classpath argument.
rmic -classpath . <Class Name>
rmic -classpath "C:\Example\Demo" <Class Name>
Here . stands for the current directory and you can specify any path there.
Try adding:
-classpath .
By default, rmic's classpath is derived from the CLASSPATH environment variable (thanks EJP for the correction).

How to compile a java program from directory?

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

Categories

Resources