【问题标题】:Illegal Argument Exception- name非法参数异常 - 名称
【发布时间】:2014-10-22 11:54:25
【问题描述】:

在过去的几天里,我一直在寻找解决这个问题的方法,但我正在把头撞到墙上。我知道用 java 编程,所以请多多包涵。

我目前正在尝试将 Java Applet 实现到我的一个学校项目的 HTML 页面中。 Applet 在使用 AppletViewer 的 Eclipse 中运行良好,在名为 Blue Jay 的程序中也可以在 Web 浏览器中运行。我已将程序导出到与我的 HTML 页面位于同一目录中的 jar 文件中,并将必要的代码添加到我的 HTML 文件中,但每当我实际运行 HTML 文件时,Applet 都会给我一个“非法参数异常:名称”错误。错误的详细信息包括短语“java.net.MalformedURLException:unknown protocol:e.”

这是我的 HTML 文件的相关代码:

<applet code="MovingBoxes.class" archive="E:\WebSystems\WebPages\Animations.jar" 
    width="350" height="350" >Animation of moving boxes</applet>

当错误发生时,小程序标签内的短语也不会显示,如果这很重要的话。我曾尝试导出其他小程序以查看它们是否工作并且每次都收到相同的错误。我也相当确定文件的目标是正确的,因为当我将目标名称更改为不正确的名称时,会发生“找不到类”错误。

如果错误出现在我的小程序中,这里是我的小程序代码。

package theBig;

import java.awt.*;

public class MovingBoxes extends java.applet.Applet implements Runnable
{
   Thread runner;
   int size = 15;
   int x_value = 200;
   int y_value = 175;
   int rndm_x;
   int rndm_y;
   int move = 1;
   int cntr = 0;

   Image dbImage;
   Graphics dbg;

   int x_value2 = 240;
   int y_value2 = 250;
   int rndm_x2;
   int rndm_y2;

   public void start() 
   {
     if (runner == null) {
          runner = new Thread(this);
          runner.start();
     }
  }

   public void stop() 
   {
     if (runner != null) {
       runner.stop();
       runner = null;
     }
   }

   public void run() 
   {
      setBackground(Color.white);
       while (true) {
       rndm_x = (int)(Math.random()*10+1);
       rndm_y = (int)(Math.random()*10+1);
       if (rndm_x > 5)
           x_value += move;
       else 
           x_value -= move;
       if (rndm_y > 5)
           y_value = 50;
       else
           y_value = 50;

       rndm_x2 = (int)(Math.random()*10+1);
       rndm_y2 = (int)(Math.random()*10+1);
       if (rndm_x2 > 5)
           x_value2 += move;
       else 
           x_value2 -= move;
       if (rndm_y2 > 5)
           y_value2 = 50;
       else
           y_value2 = 50;

       if (x_value + size > x_value2)
       {
           cntr = 50;
       }

       while(cntr > 0)
       {
           cntr --;
           x_value --;
           x_value2 ++;
           repaint();
           try { Thread.sleep(25); }
           catch (InterruptedException e) { }
       }


       repaint();
       try { Thread.sleep(25); }
       catch (InterruptedException e) { }
       }
   }


   public void update(Graphics g) {
       dbImage = createImage(getWidth(),getHeight());
       dbg = dbImage.getGraphics();
       paint(dbg);
       g.drawImage(dbImage,0,0,this);
   }

   public void paint(Graphics g) {
     g.setColor(Color.red);
     g.drawRect(x_value, y_value, size, size);
     g.fillRect(x_value, y_value, size, size);

     g.setColor(Color.blue);
     g.drawRect(x_value2, y_value2, size, size);
     g.fillRect(x_value2, y_value2, size, size);
   }   
} ` 

就像我说的,我到处寻找答案,却空手而归。非常感谢您提供的任何帮助。

【问题讨论】:

  • “MovingBoxes.class”是否存在于 CLASSPATH 中的目录中?似乎找不到“MovingBoxes.class”。
  • 我认为问题不在于代码库或存档。每当我故意输入不正确的代码库而不是 Illegal Argument Exception 错误时,就会出现 Class Not Found 错误,而 Illegal Argument Exception 错误就是代码中出现的错误。
  • 据我所知,“MovingBoxes.class”存在于 CLASSPATH 的目录中。我该如何检查?
  • 1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅this answer,了解许多放弃 AWT 使用支持 Swing 的组件的充分理由。

标签: java html applet illegalargumentexception


【解决方案1】:

也许这是一个错字,但您的 HTML 代码中的 .jar 后缺少一个 "。

如果这没有帮助,也许您的存档应该是一个 URL,而不仅仅是一个文件路径。你可以试试这个:

<applet code="MovingBoxes.class" archive="file:/E:/WebSystems/WebPages/Animations.jar" 
 width="350" height="350" >Animation of moving boxes</applet>

【讨论】:

  • 啊,我激动了一分钟,但这只是我发布问题时的一个错字,而不是在我的实际文件中。
  • 我还尝试将我的存档更改为该 URL,结果却收到了 Class Not Found 错误。
猜你喜欢
  • 2020-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多