i'm trying to compile this slick2d example (first one) but i can't get it to work. here's the code:
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
/**
* #author panos
*/
public class WizardGame extends BasicGame
{
public WizardGame()
{
super("Wizard game");
}
public static void main(String[] arguments)
{
try
{
AppGameContainer app = new AppGameContainer(new WizardGame());
app.setDisplayMode(500, 400, false);
app.start();
}
catch (SlickException e)
{
e.printStackTrace();
}
}
#Override
public void init(GameContainer container) throws SlickException
{
}
#Override
public void update(GameContainer container, int delta) throws SlickException
{
}
public void render(GameContainer container, Graphics g) throws SlickException
{
}
}
i've put lwjgl.jar, slick.jar and ibxm.jar in the directory as instructed. then i tried:
javac -classpath .;full path\slick.jar.;full path\lwjgl.jar;full path\ibxm.jar
then
java WizardGame
but i get errors that the jar classes aren't defined, which means something must be wrong when i'm setting the classpaths. i'm not sure what i'm doing wrong though.
i've also tried
javac -classpath .;.\slick.jar.;.\lwjgl.jar;.\ibxm.jar
either way, shouldn't the jvm find out about the .jar files on its own if they're on the same directory as the source code? in my case, i've set up the 3 jars in the same directory as WizardGame.java. if i try compiling without the -classpath arguments it doesn't work though.
edit:
i did as instructed, but now it's telling me "no lwjgl in java.library.path"
i examined my lwjgl.jar with winrar and saw that it contains two folders: meta-inf and org, and then inside org there is lwjgl. is it supposed to be like that? because if it is, i dunno what's wrong.
edit 2:
created the manifest with what you said, then used:
jar cfm test.jar manifest.txt
this created a jar file, then i used
java -jar test.jar
and then it told me it didn't find the definition for the class WizardGame.
You have to add the -classpath option to the java command also. Not only to the compiler.
like:
javac -classpath .;lib\myjar.jar MyClass.java
and then
java -classpath .;lib\myjar.jar MyClass
Otherwise the interpreter ( java ) won't know where to find the classes needed.
Oscar is bang on - you need the -classpath on the java command too.
Another thing you can look at (it is overkill for what you are doing now, but you should still do it for the experience) is to create a JAR file. In the JAR file you can provide a manifest.mf file that tells the java command where to find the other JAR files as well as what the main class is.
You would then run your program via "java -jar foo.jar"
http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html
http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html
I believe that the following manifest.m file will do what you want (note you have to have a blank line at the end or it will not work):
Main-Class: WizardGame
Class-Path: slick.jar lwjgl.jar ibxm.jar
That assumes that all JAR files (foo.jar and the other three) are in the same directory.
Related
I am trying to create a JAR file for my two source codes Serial.java and DBaccess.java.
I am doing this as a learning exercise ,the classes do not do anything,they are dummy classes.
OS - Windows 10
JDK - GraalVM
Listing of my source code.(Serial.java)
public class Serial
{
void open()
{
System.out.println("Serial Port Opened - 0");
}
}
Listing of my source code.(DBaccess.java)
public class DBaccess
{
String DBname = "MySerialDB";
void write()
{
System.out.println("Data Written to DB - W");
}
void read()
{
System.out.println("Data Read from DB - R");
}
}
I have compiled them to .class files using javac
javac DBaccess.java
javac Serial.java
I created a folder
H:\myclass\com\mydomain\util
and manually copied the class files to util directory
and created a jar file myjar.jar of this format
H:\myclass>H:\graalvm\bin\jar tf myjar.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/mydomain/
com/mydomain/util/
com/mydomain/util/DBaccess.class
com/mydomain/util/Serial.class
I want my source code JMainEntry.java to access the classes in the above jar file myjar.jar.
JMainEntry.java
//Main Class of the Program
import com.mydomain.util.*;
public class JMainEntry
{
public static void main (String[] Args)
{
Serial myS = new Serial();
myS.open();
DBaccess myDB = new DBaccess();
myDB.write();
myDB.read();
}
}
When i compile the code
H:\jar-compile>H:\graalvm\bin\javac -cp myjar.jar JMainEntry.java
I am getting the error
bad class file: myjar.jar(/com/mydomain/util/Serial.class)
class file contains wrong class: Serial
Please remove or make sure it appears in the correct subdirectory of the classpath.
I am not able to understand why javac is complaining like this,Can anyone help?
bad class file: myjar.jar(/com/mydomain/util/Serial.class)
Make sure you declare
package com.mydomain.util
at the top of all .java files in the directory com/mydomain/util, including Serial.java. To understand this better, read about packages.
I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping.
Problem:
Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.
I have a program that sits on my desktop named MyProgram.java:
import org.somepackage;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
Still on the Desktop, I then have another folder which I've named src, inside of which I have created the necessary subfolders corresponding to the package name, i.e. ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents:
package org.somepackage;
public class Human {
public void scream() {
System.out.println("I exist!!");
}
}
I then created a classes folder, again on the Desktop, and ran the following compile command on the command line:
javac -d ./classes/ ./src/org/packagename/Human.java
This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location.
However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e.
javac -cp ".:./classes/" MyProgram.java
As you'll see, my class path contains a reference to the current location (".") for the MyProgram.java file, and it contains a reference to the classes folder ("./classes/") which is the base location for the org.somepackage package inside whose subfolders (./classes/org/somepackage/) on can find the Human.class file.
At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error:
MyProgram.java:1: error: package org does not exist
I've been following the instructions listed here:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated.
Thanks,
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram
I'm pretty new to Java and I'm sure this is a noob question but I really can't get this to work.
I have a simple java file with some dependencies that looks like this:
package myFilePackage;
import java.io.*;
import java.nio.*;
import java.util.*;
public class myclass{
public static void modifySomeFile(arg1, arg2, ...){
//Function that uses the above dependencies
...
}
public static void main(String[] args){
for( arg : args){
System.out.println(arg);
}
//my code works fine up to here!
modifySomeFile(arg1, arg2, ...);
}
}
Basically, what happens is that it prints all the arguments just fine when I call it from the command prompt, but never calls the modifySomeFile function.
This is how I call it from the prompt:
cd C:\path\to\file java -jar myjar.jar arg1 arg2
Here's my manifest file (I read somewhere that it might be the problem)
Manifest-Version: 1.0
Class-Path: .
Main-Class: myfile.myClass
Here's the path to my class:
myfile/myClass.class
Also, there's only one class in my jar file.
I am trying to create a package of library functions for a main Java program but I am have some issues.
I don't know much about about Java packages and I am going through some documentary online.
I have created my directory as such
./Program/Program.java
./Program/TestFunc.java
./Program/classes/library/
The contents of TestFunc.java are
package library;
public class TestFunc {
public void message01() {
System.out.println("called message01");
}
public void message02() {
System.out.println("called message02");
}
}
I compiled it as I read in the documentation
javac -d ./Program/classes TestFunc.java
Which gives me
./Program/classes/library/TestFunc.class
Then I try to call it in Program.java
import library.*;
public class Program {
public static void main(String[] args) {
System.out.println("Starting Script");
}
}
When I try to compile using
javac -d ./Program/classes Program.java
I get the error
package library does not exist
Why is this?
You've used -d which says where to put the output, but you haven't told it that the same directory should also be used for input on the classpath. Use the -cp option for that:
javac -d classes -cp classes Program.java
(It's not clear whether you're trying to do this from inside the Program directory, or above it - your source filename appears to be inside the Program directory, but you're specifying the output directory as if you were in the directory above...)
I was trying to execute the following code in java:
import java.awt.*;
import javax.swing.*;
import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;
public class TextEditorDemo extends JFrame {
private static final long serialVersionUID = 1L;
public TextEditorDemo() {
JPanel cp = new JPanel(new BorderLayout());
RSyntaxTextArea textArea = new RSyntaxTextArea();
textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
RTextScrollPane sp = new RTextScrollPane(textArea);
cp.add(sp);
setContentPane(cp);
setTitle("RSyntaxTextArea 1.4 - Example 1 - Text Editor Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
public static void main(String[] args) {
// Start all Swing applications on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TextEditorDemo().setVisible(true);
}
});
}
}
Since I'm using RSyntaxTextArea file i have to give the classpath of it while I'm running the code.
Assume that my RSyntaxTextArea.jar file is in Anto(i.e. my Home directory in Ubuntu 10.10) and when I run the above code:
javac -classpath \Anto\RSyntaxTextArea.jar TextEditorDemo.java
Still I'm getting the error as RTextScrollPane could not be found kind of errors. I guess i have been giving my classpath wrongly; What to do?
Thanks for the answer.
Did you download it from the sourceforge site? It is a zip file containing the sources. Create a folder for containing the sources and unzip it. Run ant in the folder - it will create a rsyntaxtextarea.jar in the dist folder. Add this to the class path.
Assuming that /Anto is really your home directory, try this:
javac -classpath ~/RSyntaxTextArea.jar TextEditorDemo.java
Otherwise, just point to a relative path to the jar file. For one thing, you were trying to use \, where in Linux you should be using /. You can reference the current directory with . So, if the jar is in your current working directory, you can just do this:
javac -classpath RSyntaxTextArea.jar TextEditorDemo.java
Or this:
javac -classpath ./RSyntaxTextArea.jar TextEditorDemo.java
If the Anto directory is under the current directory, use this:
javac -classpath ./Anto/RSyntaxTextArea.jar TextEditorDemo.java
Because that's not the path to your home directory, nor the correct slash to use.
javac -classpath /home/Anto/RSyntaxTextArea.jar TextEditorDemo.java
Also note Java 6 allows you to use a wildcard (*) for the path to search for jar files.