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.
Related
I'm new to Java programming, so hear me out!
I write all my programs with JEdit and Notepad++ and compile and run them with the windows command line. (cd -directory- javac file.java java file etc...)
However, I've never been able to run a program file with multiple class files.
Here's the program I've been trying to test:
package JavaTests;
public class file1{
public static void main(String[] args){
Test test = new Test("Hello");
Test test2 = new Test("Good");
test.say(true);
test.newln();
test2.say(false);
System.out.println(test2.getMessage());
test2.setMessage("GoodBye");
test2.say(true);
}
}
and the 'Test' class:
package JavaTests;
public class Test{
private String message;
public Test(){
message = "I have nothing to say.";
}
public Test(String thingToSay){
message = thingToSay;
}
void say(boolean newLn){
if(newLn){
System.out.println(message);
}else{
System.out.print(message);
}
}
void setMessage(String newMessage){
message = newMessage;
}
String getMessage(){
return message;
}
void newln(){
System.out.println();
}
}
The Java files are in the folder ...\Desktop\JavaTests\
so I enter
cd ...\Desktop\JavaTests
javac file1.java Test.java
java file1
The java files compile correctly and the class files appear in the JavaTests folder, but when I try to run file1:
C:\...\Desktop\JavaTests>java file1
Error: Could not find or load main class file1
Caused by: java.lang.NoClassDefFoundError: JavaTests/file1 (wrong name: file1)
and If I try java JavaTests.file1:
C:\...\Desktop\JavaTests>java JavaTests.file1
Error: Could not find or load main class JavaTests.file1
Caused by: java.lang.ClassNotFoundException: JavaTests.file1
The Files definitely exist, and I double-checked the classpath.
Most other questions were instances of 'exception in the thread Main'. I can't figure out what's wrong. Can anyone help? Thanks!
In java, the norm is to name your class with a Capital first letter. So instead of file1, name it File1
e.g.
public class File1 {
And save it with the same name i.e. File1.java .
Since you are keeping all the classes in the same package, write all classes in the same package in the same file and save it in the name of the driver class (the class with the main method which is File1.java). Then compile it i.e.
javac File1.java
After compilation, you would notice the generated classes i.e. File1.class and Test.class.
Then you can run your program by executing the driver class i.e.
java File1
edit : Here's some detail ,
Open your File1.java ,
make sure your driver class is File1 .
copy the contents of Test class and paste it on File1.java just after the driver declaration.
e.g.
public class File1 {
//codes here
}
class Test {
//codes here
}
Save it
I also noticed just now that your package name starts with a capital letter. The norm is that package names SHOULD NOT contain Capital letters. So you can rename the package name to contain only small letters.
Also make sure, this file (File1.java ) contains in folder/directory of its own.
Then compile it.
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 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...)
There are a alot of questions about javah, but I couldnt find any solution for my issue for 3 day.
My java file work normally and no error.
I copied my java file "I2CInterface.java" to "jdk/bin" directory.
Then "javac I2CInterface.java" the I2CInterface.class created succesfully.
But "javah -jni I2CInterface" is not work the header file cant created. The error is "class not found" I try with classpath but not work. I set my environtmental and add path C:\Program Files\Java\jdk1.8.0\bin. No work.
That is interesting javah work on some class and it can create header. But on this class and some class not work.
The problem is about java file? My java file below.
package com.multitek.ipintercomflatunit;
public class I2CInterface {
private static native int i2cwrite(byte[] data);
private static native byte[] i2cread(int data_len);
public static int write(byte[] data) {
return(i2cwrite(data));
}
public static byte[] read(int data_len) {
return(i2cread(data_len));
}
static
{
System.loadLibrary("i2cinterface");
}
If the class is in a package, say xxx, the correct command line will be
javah xxx.I2CInterface
I copied my java file "I2CInterface.java" to "jdk/bin" directory
Why? There's no need to do that. Don't pollute the Java installation directory with your own stuff. Leave it where it was and compile it there. Adjust your PATH if necessary so you can execute the JDK tools.
I have created a package:
path : /home/myid/py_ejb
File : XmppMnager.java
package xmpp;
import org.jivesoftware.smack.Chat;
public class XmppManager {
}
Compiled with
javac -d . -classpath .:smack.jar XmppManager.java
File: XmppTest.java
import xmpp.*;
public class XmppTest {
public static void main(String[] args) throws Exception {
String username = "testuser1";
String password = "testuser1pass";
XmppManager xmppManager = new XmppManager("myserver", 5222);
..}
Compiled with
$ javac -classpath .:smack.jar:./xmpp XmppTest.java
XmppTest.java:10: cannot access XmppManager
bad class file: RegularFileObject[./xmpp/XmppManager.class]
class file contains wrong class: xmpp.XmppManager
Please remove or make sure it appears in the correct subdirectory of the classpath.
XmppManager xmppManager = new XmppManager("myserver", 5222);
^
1 error
I tried a lot of way to fix this compilation issue but it just does not go away
Move the source files into a folder named xmpp so that the package names match that of the folder
Package names are directly related to the classpath directory structure. All the classes in the xmpp package need to be in a folder named xmpp, and this folder must be on the classpath. Similarly, if you had a package called xmpp.util.io you would have to put the files in xmpp/util/io/.
The usual convention is to make a src directory to hold all your source files, and then that can be filled with a directory structure that exactly matches your package structure. A pretty decent tutorial on packages can be found here.
Also, it looks like this is probably just a typo in the question, but if your file is actually named XmppMnager.java rather than XmppManager.java, that won't compile either.