How to import classes in Java without eclipse? - java

I'm sorry that this may seem like a basic question, but I need help on importing classes. I downloaded the Lightweight Java Gaming Library(LWJGL), but I don't know how to install it. When I try the test, it doesn't work.
I have a folder in which I extracted the basic LWJGL files. I now have a setup like this:
generic_folder/libs/lwjgl-2.8.3
I also have this test which I copy-pasted from the tutorial on the LWJGL wiki:
It is located in the generic_folder. When I try and javac it, it says
DisplayExample.java:1: package org.lwjgl does not exist.
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class DisplayExample {
public void start() {
try {
Display.setDisplayMode(new DisplayMode(800,600));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
// init OpenGL here
while (!Display.isCloseRequested()) {
// render OpenGL here
Display.update();
}
Display.destroy();
}
public static void main(String[] argv) {
DisplayExample displayExample = new DisplayExample();
displayExample.start();
}
}
Do I have to place the files in a certain location or what? Please help. Thanks!
Edit: I am sorry. I'm new to Java. Please go a little slower. Also, LWJGL is a library, so I didn't place any files inside of it.

You need to specify a classpath (preferrably with the -cp switch) such that java/javac knows where to find the .jar files you need access to. It typically looks as follows:
For compiling:
javac -cp .:libs/lwjgl-2.8.3/jar/lwjgl.jar your-java-files
for running:
java -cp .:libs/lwjgl-2.8.3/jar/lwjgl.jar your-class-file
(Note that if you're using Windows, I believe : should be replaced by ;.)

The easiest way is to set the LWJGL folder to CLASSPATH environment. For example
set CLASSPATH=${PATH_TO}/generic_folder/libs/lwjgl-2.8.3
That should do it.

You need to add that jar to your classpath when you invoke javac, like so:
javac -cp generic_folder/libs/lwjgl-2.8.3.jar path/to/java/file/DisplayExample.java
I'm assuming that lwjgl-2.8.3 is actually a jar file. You never specified in your question.
You'll have to once again add the jar to your classpath when you attempt to run your example:
java -cp generic_folder/libs/lwjgl-2.8.3.jar path/to/java/file/DisplayExample

When you run javac, you need to tell it where the jar file is. You can either set you CLASSPATH environment variable or pass the location to the java compiler. The latter is better, as you probably won't want lwjgl if you start a second java project.
To tell the java compiler directly, you would do something like
javac -classpath generic_folder/libs/lwjgl-2.8.3/lwjgl.jar DisplayExample.java
The classpath can either point to a jar file containing the classes or the top level directory containing unjared class files. If the unjarred classes are in generic_folder/libs/lwjgl-2.8.3/classes, you would point to generic_folder/libs/lwjgl-2.8.3/classes, not generic_folder/libs/lwjgl-2.8.3/org/lwjgl

javac -classpath external.jar myClass.java
For more info -> https://stackoverflow.com/a/5112638/5790669

Related

java class executing on Eclipse but not in command line

My java class files run in Eclipse but not in command line. I have tried all possible solutions. My code has the following structure:
Client_1/src/filedownload/Client.java
RMI_interface/src/filedownload/Hello.java
The Client.java file is dependent on Hello.java. filedownload is the name of package.
When I compile using the following command, it works.
javac RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
But when I execute the class file in the Client_1/src folder using following command, it does not work.
java filedownload.Client
The error displayed is
Could not find or load main class
I have tried many posts on stackoverflow but I am unable to solve it. I am using ubuntu.
The code structure is
package filedownload;
import ....
public class Client implements Hello, Runnable{
...other functions.....
public static void main(String args[])throws Exception{
}
}
Does your Client class have the main() method ? Where are the .class files after compilation (that is, what's the current directory you're executing the compile from) ? What's the current directory when you try to execute ? What's the classpath when you try to execute ?
Without all that info, there's little chance of anyone being able to get you going (but for the obvious advice of just setting up eclipse and doing everything from within eclipse - letting eclipse take care of all the nitty gritty detail).
(And the questions themselves suggest various possible points of failure in your scenario so look at it.)
All your steps seems to be correct. You didn't share the Client.java code which has main method.
Make sure you follow this main method syntax:
public static void main(String[] args){
...
}
E.g. if you write main without args, it can't be found.
You need to put your classes in a separate folder, separated from your sources.
javac -d bin RMI_interface/src/filedownload/Hello.java Client_1/src/filedownload/Client.java
(folder 'bin' must exist already)
And inside folder 'bin' execute command:
java filedownload.Client

Java could not find or load main class error

For some reason, I can't get java to run my program. Whenever I try I get the message
"Error: Could not find or load main class Project"
In Command Prompt I type cd Documents since the file is in my Documents folder, type
javac Project.java
then
java Project
to try and run it but I get the above error message.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Project
{
public static void main(String[] args)
{
Code and stuff
}
}
There's a fair bit of code that I left out but I think this is the part that's messed up. Let me know if you need to see the rest of the code and I'll edit this.
Change
java Project
to (assuming Project.class is in your current folder)
java -cp . Project
as it is, you aren't setting a class-path.
You have add the path of .class files in classpath during execution
Run following command:
java -classpath C:\Users\DELL\Documents Project

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 can I create a Class file and JAR file without main function?

How can I create the class file and jar file for this coding, when I compile this program its not working because there is no main function in the program. And also I am trying in command prompt but I don't know how to set the classpath? please help me
My Coding is here
public class NewLogFields implements ILogNotify
{
public void onLog(Level level, String comment, IMediaStream stream, String category,String event, int status, String context) {
if (category.equals(WMSLoggerIDs.CAT_session) && event.equals(WMSLoggerIDs.EVT_destroy))
{
Long csBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_cs_bytes);
Long scBytes = (Long)WMSLoggerFactory.getGlobalLogValue(WMSLogger IDs.FD_sc_bytes);
System.out.println("disconnect: csBytes:"+csBytes+" scBytes:"+scBytes);
}
}
}
Once you compiled the coding in wowza media Serever the jar file is automatically created in the library folder,see your Installation Library folder.Still you have problem Gothrough this link Wowza Quick Guide
What do you want to do?
Create a class and an jar file out of this Java code so that you can use this in another Java program?
Then you have to compile it:
java NewLogFields.java
Looks like you are unable to compile it at all. This could be because the interface ILogNotify (or the jar that contains this) is not in the classpath.
You can include the path/jar containing this interface in the classpath by using:
javac -cp .;path_to_jar_or_class NewLogFields.java
where path_to_jar_or_class is the path to the folder or jar file that contains ILogNotify.
For example, this may be something like ./logNotify.jar or ./log/
You can set use switch -cp or -classpath with javac command.
for example javac -cp path and name of jat file or class file yourjavafile.java
create the class file using the compiler: javac NewLogFileds.java
create the jar file using the jar command: jar cvf stuff.jar NewLogFileds.class
You are correct that the program needs a main() function in order to run.
add:
public static void main(String args[]) {
// code here
}
With that you could run the code with or without the jar:
java NewLogFields
or
java -cp stuff.jar NewLogFields
There are ways of creating a MANIFEST file that tells java which class to run from the jar making the last line more simple.
The link that you provided tells you how to do it:
Compile your class in the normal way.
Create a JAR file containing your class in the normal way.
Copy the JAR file into the wonza installation's lib as described in the javadoc.
Edit the startup script to add the -Dcom.wowza.wms.logging.LogNotify=... option to JAVA_OPTS ... as described in the javadoc.
The "full-path-to-your-ILogNotify-class" is actually supposed to be the fully qualified class name of your class; it is obvious from the examples.
Edit WowzaMediaServerPro-Service.conf and log4j.properties as described in the javadoc.
Restart the server.
If you put your JAR file in the directory like the instructions tell you to, you won't need to modify the classpath by changing -cp argument.
Your class doesn't need a main method because it is not a free-standing application. Rather, it is a "plugin" class that gets dynamically loaded and instantiated by the Wowza core as required. The "-D..." option and the config file change tell the Wonza core which class to try to load.

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