Your java file needs to be in a directory reflecting the package name:
net/utils/image/MainSplitter.java
$ javac net/utils/image/*.java
$ java -cp . net.utils.image.MainSplitter
Question
These are my two .java, the fisrt one:
package net.utils.image;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.imageio.ImageIO;
public class BufferedImageSplitter {
protected BufferedImage image;
protected BufferedImage[] splittedImages;
public static BufferedImageSplitter getInstance( String path ) throws IOException, FileNotFoundException {
File file = new File( path );
FileInputStream fis = new FileInputStream(file);
return new BufferedImageSplitter( fis );
}
public BufferedImageSplitter(InputStream image) throws IOException {
this.image = ImageIO.read(image);
}
public BufferedImageSplitter(FileInputStream image) throws IOException {
this.image = ImageIO.read(image);
}
public BufferedImageSplitter(File image) throws IOException {
this.image = ImageIO.read( image );
}
public BufferedImageSplitter(URL image) throws IOException {
this.image = ImageIO.read( image );
}
public BufferedImage[] split(int height) {
int width = this.image.getWidth();
int rows = (int) Math.ceil( (float) this.image.getHeight() / (float) height );
this.splittedImages = new BufferedImage[rows]; // Image array to hold
// image chunks
for (int count = 0; count < rows; count++) {
Graphics2D gr = null;
int minHeightOffset = height * count;
int maxHeightOffset = Math.min( image.getHeight() , height * count + height );
int splitHeight = maxHeightOffset - minHeightOffset;
// Initialize the image array with image chunks
this.splittedImages[count] = new BufferedImage(width, splitHeight,
this.image.getType());
// draws the image chunk
gr = this.splittedImages[count].createGraphics();
gr.drawImage(this.image, 0, 0, width, splitHeight, 0,
minHeightOffset, width, maxHeightOffset, null);
gr.dispose();
}
return this.splittedImages;
}
public void saveFiles(String FormatName, String ImageName ) throws IOException {
//writing mini images into image files
for (int i = 0; i < this.splittedImages.length; i++) {
String padding = String.format("%03d", i);
ImageIO.write(this.splittedImages[i], FormatName, new File( ImageName + padding + "." + FormatName));
}
}
}
And the second one which contains the main class:
package net.utils.image;
import java.io.*;
public class MainSplitter {
public static void main(String[] args) throws IOException {
// File file = new
// File("C:\\Documents and Settings\\Administrador\\Mis documentos\\b12.png");
// FileInputStream fis = new FileInputStream(file);
// BufferedImageSplitter imageSplitter = new BufferedImageSplitter(fis);
if (args.length != 3) {
System.out.println("Parametros incorrectos. Inserta"
+ " <path imagen> <formato imagen> <nombre imagen>");
System.exit(0);
}
String path_img = args[0];
String format = args[1];
String name_img = args[2];
BufferedImageSplitter imageSplitter = BufferedImageSplitter
.getInstance(path_img);
imageSplitter.split(500);
imageSplitter.saveFiles(format, name_img);
}
}
And this is what I have tried so far without success:
My structure of the files is: /root and there I have both .java
1) 1.1 javac *.java (it compiles right) 1.2 java MainSplitter Exception in thread "main" java.lang.NoClassDefFoundError: MainSplitter (wrong name: net/utils/image/MainSplitter)
2) 2.1 javac -cp . *.java
2.2 java cp . *.java
Again the same error
3) 3.1 javac *.java 3.2 java java net.utils.image.MainSplitter.main Error: Could not find or load main class net.utils.image.MainSplitter.main
4) I have done a restructure of the files and put both .java in this directory: root/net/utils/image and then:
4.1 javac -cp . *java java -cp . MainSplitter Again the first error: Exception in thread "main" java.lang.NoClassDefFoundError: MainSplitter (wrong name: net/utils/image/MainSplitter).
I might have a problem with cp but don't really know, probably is easy but I am stuck, can't execute it. Thank you in advance, I would appreciate any help
Solution
Your java file needs to be in a directory reflecting the package name:
net/utils/image/MainSplitter.java
$ javac net/utils/image/*.java
$ java -cp . net.utils.image.MainSplitter
OTHER TIPS
Hi you compile MainSplitter
first.
Then check you have same package which holds your both classes.
I hope this help.