I made a program using Eclipse, but I want to learn to compile it with JavaC.
I downloaded the JavaSDK jdk1.7.0 and installed it at C:\
My .java file is in C:\java
I then compiled the program using C:\jdk1.7.0\bin\javac Game.java
It uses a package layout manager so I downloaded swing-layout-1.0.1.jar and just put it straight into C:\ then tried to execute using:
java -classpath C:\swing-layout-1.0.1.jar Game
All I get is Error:Could not find or load main class Game
Any idea what I'm doing wrong?
You forgot to add your code to classpath. You should provide either jar file or directory where your classes are located, i.e.
java -classpath C:\swing-layout-1.0.1.jar;myapp.jar Game
or
java -classpath C:\swing-layout-1.0.1.jar;c:\proj\myapp\classes Game
If you know the name of your main class you can try
java -classpath .;C:\swing-layout-1.0.1.jar; your.package.MainClass
Related
I have a simple Java Maven program that I created in Intellij. It has Two classes Main and Read. I was able to build a jar and run it. However if I zip the source code into a folder , would I be able to compile it using command line? Something like javac ? How should I do this? Shall I run commands in Java folder in the project? Many thanks.
As far as the Java class loader is concerned, a .jar or .zip file is the same as a directory containing the the files, and .jar and .zip files are generally used to distribute compiled Java packages. Here you can read more about it.
For compiling the zip file: use the -classpath option to javac and java. We could, for example:
javac -classpath .:/users/johnr/java:/opt/jdk1.1.6/lib/classes.zip Hello.java
I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files
I am using Linux Mint, Apache ANT and a command line.
I am trying to write a game using LWJGL and am also developing a personal utility library at the same time. I am trying to create a runnable jar file of this game. Using ANT I have created a jar file with all of my classes for the game and a manifest specifying the main class to run and a classpath. My problem is that when I run the game using
java -cp library.jar -jar game.jar
I get a class not found error on a class from library.jar.
If I run the game main class by going
java -cp bin:library.jar game.MainClass
The game runs fine, but the jar won't work.
I have expanded the jar file and read the manifest, the class files are all there with the right directory structure (no bin directory, starts with game) and the manifest seems fine. I've tried including the current directory into the classpath (.:library.jar) and I have tried giving a full path to the jar rather than a local path.
Besides actually expanding the entire library.jar into my game.jar how can I fix this? I can give more specific directory information if necessary.
I made an application using bluetooth and so I had to use external libraries (bluecove). I am now looking to create a single executable to be able to give it to testers. I tested the executable JAR and it does not work, so I flip on a basic script.
I created a folder with all the java files inside and my external library and a folder with my pictures in it.
when I compile I have no error:
javac-cp bluecove-2.1.0.jar *. java
But in execution:
java -cp bluecove-2.1.0.jar Main
it gives me "error: Could not find or load main class Main"! I'm sur that my class contain my Main function Main.
What strange me is that in eclipse everything goes perfectly but not outside.
All those who have an idea and a hand to me is to thank in advance;)
java -cp "bluecove-2.1.0.jar;." Main
linux:
java -cp "bluecove-2.1.0.jar:." Main
Note the . which indicates the current directory, it must be also on the classpath so that your class Main can be loaded.
I want to create our own jar which has some simple methods like a print method.
I created the jar of that java project and imported it into an other project I am building path also.
The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class.
i am using eclipse 3.4 (Eclipse Ganymede (3.4))version
Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.
To compile:
javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java
To Run:
java -cp .:PathToMyJar/MyJar.jar MyMainClass
The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.
If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.