Eclipse does not recognize package Import? JOGL - java

i have jogl in the path and this program works fine until i include "import net.java.games.jogl.*;" on the top of the code. I get error "Import cannot be resolved" As i told you it works without the import. The Native Libraries are installed and jogl.jar is installed. Why isnt Eclipse recognizing this package import? here is the code:
import net.java.games.jogl.*;
public class HelloWorld
{ // open HelloWorld
public static void main(String[] args)
{ // open main
try
{ // open try
System.loadLibrary("jogl");
System.out.println("Hello World! (The native libraries are installed.)");
} // close try
catch (Exception e) // all try's need a catch
{ } // even if the catch does nothing
} // close main
} // close HelloWorld

It appears that the example you are working from is using an outdated package name. Take a look at the contents of the jar to determine the correct package.
This link (from 2006) is suggesting you may want to look at the package javax.media.opengl. Also, here is some javadoc for jogl that I found. I am not sure how up to date it is though...

I am not sure if you are using Maven or not but check your Java Build Path from inside Eclipse. Under Libraries tab ensure that JOGL is listed.

Related

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

Third party JAR's in java

I included the 3rd party jar file in the java project but still this is showing compile time error.
I included the path of the jar file in add external jar's in java.
package com.aamir;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;//compile time error occurs here.
public class ReadCSV3 {
public static void main(String[] args)
{
CSVReader reader = null;
try
{
//Get the CSVReader instance with specifying the delimiter to be used
reader = new CSVReader(new FileReader("SampleCSVFile.csv"),',');
String [] nextLine;
//Read one line at a time
while ((nextLine = reader.readNext()) != null)
{
for(String token : nextLine)
{
//Print all tokens
System.out.println(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Your problem is that you are including the source only JAR file. That's the problem right there. You have the sources only (uncompiled source) JAR file and not the binary JAR (it must contain the .class files). You need opencsv-2.2.jar instead.
Here is a zip file of the binary JAR.
I included the path of the jar file in add external jar's in java.
You need to add it to the project.
You didn't mention exactly how to added the jar. Can you please post the screenshot of
Right click project --> Build Path --> Configure --> Libararis (3rd Tab ) --> Add External Jar
You should add the jar here. Please do so if not already done.
What development tools are you using? Are you compiling at the command line or in an IDE?
If at the command line, you need to include the third party JAR in the CLASSPATH - either as an environment variable or as an argument to javac.
If in an IDE such as Eclipse, you need to specify the add the JAR to the build path in the project.
Firstly, just for sanity's sake, double-check that the JAR is in the build path - that you have given it the right name and the right path - you should see it in the Referenced Libraries section of your Java project if you expand it in the Project View. Try removing it and adding it again.
Then, secondly, check that the class you are importing exists - that you have got the right package name and class name (it is case sensitive). You should be able to expand the JAR in Eclipse and see the packages and classes inside it.
Thirdly, if you are certain that everything is correct, try to use Eclipse's auto-import feature - delete your import line and press CTRL-SHIFT-O (for Organise Imports) and see if it imports the right class.
If it is not auto-importing, then it indicates that the class is not in the class path. Try steps 1 and 2 again.
Also, try cleaning Project --> Clean --> Clean all projects

Running a Java code in cmd is not working while everything is fine in Eclipse

I wrote the code below in Eclipse and trying to do the same in cmd. However I in cmd the error message :
Error: Could not find or load main class GetMousePosition.
In cmd, I am in the folder where they class is : c:\Java\Examples\src\Robots\
When I compile the class (doing : javac GetMousePosition), everything works fine.
Originally, I set up my java's bin folder path in the environment variables.
Thanks in advance for your help
package Robots;
import java.awt.*;
import java.awt.event.*;
public class GetMousePosition {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(MouseInfo.getPointerInfo().getLocation());
}
}
You need to provide the full class name on the command line, in this case Robots.GetMousePosition. java may also assume that your class is in the right directory structure, so you should probably move up one directory.
You need to back up one directory (to c:\Java\Examples\src\) and then run
java Robots.GetMousePosition
The problem is that you need to specify the main class. Eclipse can not find the class that has a main method.
For this you need to
Right click the file in the view -> run as -> Run configuration and there you need to select the class that has main method.
For detailed guide follow this: Set Eclipse launch configuration

How to import classes in Java without eclipse?

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

Setting up path Connector/J in windows 7

Can anyone please tell me as to how to setup the path for Connector/j using the "mysql-connector-java-5.1.18-bin.jar" in windows 7?
I'm using the below code and it always end up throwing an exception.
(java.lang.ClassNotFoundException : com.mysql.jdbc.driver)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Connection working");
}
catch (Exception ex)
{
System.out.println("Connection Fail");
System.out.println(ex.getMessage());
}
}
}
I tried following the official documentation of connector/j for setting up the path, but could not understand.
I tried adding E:\PROGRAM FILES\JAVA\jdk1.7.0_01\jre\lib\ext\mysql-connector-java-5.1.18-bin.jar in the "path" environment variable, Please correct me.
First, you dont need newInstance on Class.forName("com.mysql.jdbc.Driver")
Second don't copy jars to your JDK folder, there shouldn't ever bee a need to copy them there. The correct thing to do is add the jar to your project as a dependency. If your not using an IDE, then you want to add the jar to java.exe as a --classpath option when you run your code (run "java.exe /?" for more details). If you're using eclipse, you should add the mysql jar to the project by clicking on the project and selecting "properties" and then "Java Build Path" there will be an "Add JARs..." button on the right. Then the IDE will add it to your classpath automatically.

Categories

Resources