gpt4 book ai didi

java 。编译/执行 2 .java 1 包命令行

转载 作者:行者123 更新时间:2023-12-02 05:55:59 25 4
gpt4 key购买 nike

这是我的两个 .java,第一个:

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));
}
}

}

第二个包含主类:

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);

}
}

这是我迄今为止尝试过但没有成功的方法:

我的文件结构是:/root,我有两个 .java

1) 1.1 javac *.java (编译正确) 1.2 java主分割器 线程“main”中出现异常java.lang.NoClassDefFoundError:MainSplitter(错误名称:net/utils/image/MainSplitter)

2) 2.1 javac -cp 。 *.java 2.2 java cp 。 *.java
再次出现同样的错误

3) 3.1 javac *.java 3.2 java net.utils.image.MainSplitter.main错误:无法找到或加载主类 net.utils.image.MainSplitter.main

4)我对文件进行了重组,并将两个 .java 放入此目录中:root/net/utils/image,然后:

4.1 javac -cp 。 *java java -cp 。主分配器 再次出现第一个错误:线程“main”java.lang.NoClassDefFoundError中的异常:MainSplitter(错误名称:net/utils/image/MainSplitter)。

我可能有 cp 问题,但真的不知道,可能很容易,但我被卡住了,无法执行它。预先感谢您,我将不胜感激任何帮助

最佳答案

您的 java 文件需要位于反射(reflect)包名称的目录中:

net/utils/image/MainSplitter.java

$ javac net/utils/image/*.java

$ java -cp . net.utils.image.MainSplitter

关于 java 。编译/执行 2 .java 1 包命令行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23082011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com